<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Mongo,Mongo索引," />








  <link rel="shortcut icon" type="image/x-icon" href="/blog/favicon.ico?v=5.1.0" />






<meta name="description" content="上一篇中，我们了解了Mongo如何对查询进行分析的。这章，我们通过上一章的知识，来分析下Mongo的索引规则，以便在日后工作开发中创建性能更优的索引。 我们在people这个集合中来实验我们索引的规则。people这个controller主要保存了，我们的人员信息：姓（first_name）,名（last_name）, email, 生日， 地址，公司，老板等信息，下面就让我们看下大概的数据结构以">
<meta name="keywords" content="Mongo,Mongo索引">
<meta property="og:type" content="article">
<meta property="og:title" content="Mongo查询分析以及索引规则二">
<meta property="og:url" content="https://gitee.com/shuhao/2017/12/16/Mongo查询分析以及索引规则二/index.html">
<meta property="og:site_name" content="Niki">
<meta property="og:description" content="上一篇中，我们了解了Mongo如何对查询进行分析的。这章，我们通过上一章的知识，来分析下Mongo的索引规则，以便在日后工作开发中创建性能更优的索引。 我们在people这个集合中来实验我们索引的规则。people这个controller主要保存了，我们的人员信息：姓（first_name）,名（last_name）, email, 生日， 地址，公司，老板等信息，下面就让我们看下大概的数据结构以">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-01-06T07:54:45.657Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Mongo查询分析以及索引规则二">
<meta name="twitter:description" content="上一篇中，我们了解了Mongo如何对查询进行分析的。这章，我们通过上一章的知识，来分析下Mongo的索引规则，以便在日后工作开发中创建性能更优的索引。 我们在people这个集合中来实验我们索引的规则。people这个controller主要保存了，我们的人员信息：姓（first_name）,名（last_name）, email, 生日， 地址，公司，老板等信息，下面就让我们看下大概的数据结构以">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Mist',
    sidebar: {"position":"right","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://gitee.com/shuhao/2017/12/16/Mongo查询分析以及索引规则二/"/>





  <title> Mongo查询分析以及索引规则二 | Niki </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?cb9f0585e406f878cb2f29c89ad3cdb7";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>










  
  
    
  

  <div class="container one-collumn sidebar-position-right page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Niki</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">天道酬勤</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-categories"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/shuhao/blog/2017/12/16/Mongo查询分析以及索引规则二/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Fang ShuHao">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/head.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Niki">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Mongo查询分析以及索引规则二
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-12-16T15:23:27+08:00">
                2017-12-16
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/Mongo/" itemprop="url" rel="index">
                    <span itemprop="name">Mongo</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p><a href="http://shuhao.gitee.io/blog/2017/12/07/Mongo%E6%9F%A5%E8%AF%A2%E5%88%86%E6%9E%90%E4%BB%A5%E5%8F%8A%E7%B4%A2%E5%BC%95%E8%A7%84%E5%88%99/" target="_blank" rel="external">上一篇</a>中，我们了解了Mongo如何对查询进行分析的。这章，我们通过上一章的知识，来分析下Mongo的索引规则，以便在日后工作开发中创建性能更优的索引。</p>
<p>我们在people这个集合中来实验我们索引的规则。people这个controller主要保存了，我们的人员信息：姓（first_name）,名（last_name）, email, 生日， 地址，公司，老板等信息，下面就让我们看下大概的数据结构以及其索引情况。</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&gt; db.people.getIndexes()</span><br><span class="line">[</span><br><span class="line">        &#123;</span><br><span class="line">                &quot;v&quot; : 2,</span><br><span class="line">                &quot;key&quot; : &#123;</span><br><span class="line">                        &quot;_id&quot; : 1</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;name&quot; : &quot;_id_&quot;,</span><br><span class="line">                &quot;ns&quot; : &quot;m201.people&quot;</span><br><span class="line">        &#125;</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line">&gt; db.people.findOne().pretty()</span><br><span class="line">&#123;</span><br><span class="line">        &quot;_id&quot; : ObjectId(&quot;57d7a121fa937f710a7d4874&quot;),</span><br><span class="line">        &quot;last_name&quot; : &quot;Johnson&quot;,</span><br><span class="line">        &quot;quote&quot; : &quot;Esse quaerat suscipit voluptate ipsam magni tempore accusamus.&quot;,</span><br><span class="line">        &quot;job&quot; : &quot;Sales executive&quot;,</span><br><span class="line">        &quot;ssn&quot; : &quot;081-78-3110&quot;,</span><br><span class="line">        &quot;address&quot; : &#123;</span><br><span class="line">                &quot;city&quot; : &quot;Anthonyhaven&quot;,</span><br><span class="line">                &quot;street&quot; : &quot;41441 Galvan Lights Apt. 820&quot;,</span><br><span class="line">                &quot;zip&quot; : &quot;53666&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;first_name&quot; : &quot;Martin&quot;,</span><br><span class="line">        &quot;company_id&quot; : ObjectId(&quot;57d7a121fa937f710a7d486d&quot;),</span><br><span class="line">        &quot;employer&quot; : &quot;Terry and Sons&quot;,</span><br><span class="line">        &quot;birthday&quot; : ISODate(&quot;2012-01-21T08:13:29Z&quot;),</span><br><span class="line">        &quot;email&quot; : &quot;sboyd@yahoo.com&quot;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="单索引"><a href="#单索引" class="headerlink" title="单索引"></a>单索引</h2><p>我们首先需要根据姓名（last<em>name）来查询人员，我们来看 </em>没有索引<em> 和 </em>使用索引_ 的情况(我们采用executionStats模式来分析)</p>
<h6 id="无索引查询Johnson"><a href="#无索引查询Johnson" class="headerlink" title="无索引查询Johnson"></a>无索引查询Johnson</h6><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&gt; explain.find(&#123;last_name:&quot;Johnson&quot;&#125;)</span><br><span class="line">&#123;</span><br><span class="line">        &quot;queryPlanner&quot; : &#123;</span><br><span class="line">                &quot;plannerVersion&quot; : 1,</span><br><span class="line">                &quot;namespace&quot; : &quot;m201.people&quot;,</span><br><span class="line">                &quot;indexFilterSet&quot; : false,</span><br><span class="line">                &quot;parsedQuery&quot; : &#123;</span><br><span class="line">                        &quot;last_name&quot; : &#123;</span><br><span class="line">                                &quot;$eq&quot; : &quot;Johnson&quot;</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;winningPlan&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;COLLSCAN&quot;,</span><br><span class="line">                        &quot;filter&quot; : &#123;</span><br><span class="line">                                &quot;last_name&quot; : &#123;</span><br><span class="line">                                        &quot;$eq&quot; : &quot;Johnson&quot;</span><br><span class="line">                                &#125;</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;direction&quot; : &quot;forward&quot;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;rejectedPlans&quot; : [ ]</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;executionStats&quot; : &#123;</span><br><span class="line">                &quot;executionSuccess&quot; : true,</span><br><span class="line">                &quot;nReturned&quot; : 794,</span><br><span class="line">                &quot;executionTimeMillis&quot; : 37,</span><br><span class="line">                &quot;totalKeysExamined&quot; : 0,</span><br><span class="line">                &quot;totalDocsExamined&quot; : 50475,</span><br><span class="line">                &quot;executionStages&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;COLLSCAN&quot;,</span><br><span class="line">                        &quot;filter&quot; : &#123;</span><br><span class="line">                                &quot;last_name&quot; : &#123;</span><br><span class="line">                                        &quot;$eq&quot; : &quot;Johnson&quot;</span><br><span class="line">                                &#125;</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;nReturned&quot; : 794,</span><br><span class="line">                        &quot;executionTimeMillisEstimate&quot; : 20,</span><br><span class="line">                        &quot;works&quot; : 50477,</span><br><span class="line">                        &quot;advanced&quot; : 794,</span><br><span class="line">                        &quot;needTime&quot; : 49682,</span><br><span class="line">                        &quot;needYield&quot; : 0,</span><br><span class="line">                        &quot;saveState&quot; : 394,</span><br><span class="line">                        &quot;restoreState&quot; : 394,</span><br><span class="line">                        &quot;isEOF&quot; : 1,</span><br><span class="line">                        &quot;invalidates&quot; : 0,</span><br><span class="line">                        &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                        &quot;docsExamined&quot; : 50475</span><br><span class="line">                &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;serverInfo&quot; : &#123;</span><br><span class="line">                &quot;host&quot; : &quot;YZJC1606-0218&quot;,</span><br><span class="line">                &quot;port&quot; : 27017,</span><br><span class="line">                &quot;version&quot; : &quot;3.4.7&quot;,</span><br><span class="line">                &quot;gitVersion&quot; : &quot;cf38c1b8a0a8dca4a11737581beafef4fe120bcd&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;ok&quot; : 1</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong><em>从上面的分析看到，我们扫描了50475(executionStats.totalDocsExamined，查询到了794（executionStats.nReturned）条名字Johnson的人员信息，查询总共共花费37毫秒（executionStats.executionTimeMillis）；</em></strong><br><strong><em>查询的策略是全表扫描：COLLSCAN（executionStats.executionStats.stage），全表扫描查询共花费时间20ms(executionStats.executionStats.executionTimeMillisEstimate)</em></strong><br>这显然不是我们希望看到的查询效率。</p>
<h6 id="添加索引last-name-查询Johnson"><a href="#添加索引last-name-查询Johnson" class="headerlink" title="添加索引last_name,查询Johnson"></a>添加索引last_name,查询Johnson</h6><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&gt; db.people.createIndex(&#123;last_name:1&#125;)</span><br><span class="line">&#123;</span><br><span class="line">        &quot;createdCollectionAutomatically&quot; : false,</span><br><span class="line">        &quot;numIndexesBefore&quot; : 1,</span><br><span class="line">        &quot;numIndexesAfter&quot; : 2,</span><br><span class="line">        &quot;ok&quot; : 1</span><br><span class="line">&#125;</span><br><span class="line">&gt; db.people.getIndexes()</span><br><span class="line">[</span><br><span class="line">        &#123;</span><br><span class="line">                &quot;v&quot; : 2,</span><br><span class="line">                &quot;key&quot; : &#123;</span><br><span class="line">                        &quot;_id&quot; : 1</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;name&quot; : &quot;_id_&quot;,</span><br><span class="line">                &quot;ns&quot; : &quot;m201.people&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">                &quot;v&quot; : 2,</span><br><span class="line">                &quot;key&quot; : &#123;</span><br><span class="line">                        &quot;last_name&quot; : 1</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;name&quot; : &quot;last_name_1&quot;,</span><br><span class="line">                &quot;ns&quot; : &quot;m201.people&quot;</span><br><span class="line">        &#125;</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line">&gt; explain.find(&#123;last_name:&quot;Johnson&quot;&#125;)</span><br><span class="line">&#123;</span><br><span class="line">        &quot;queryPlanner&quot; : &#123;</span><br><span class="line">                &quot;plannerVersion&quot; : 1,</span><br><span class="line">                &quot;namespace&quot; : &quot;m201.people&quot;,</span><br><span class="line">                &quot;indexFilterSet&quot; : false,</span><br><span class="line">                &quot;parsedQuery&quot; : &#123;</span><br><span class="line">                        &quot;last_name&quot; : &#123;</span><br><span class="line">                                &quot;$eq&quot; : &quot;Johnson&quot;</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;winningPlan&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;FETCH&quot;,</span><br><span class="line">                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                &quot;stage&quot; : &quot;IXSCAN&quot;,</span><br><span class="line">                                &quot;keyPattern&quot; : &#123;</span><br><span class="line">                                        &quot;last_name&quot; : 1</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;indexName&quot; : &quot;last_name_1&quot;,</span><br><span class="line">                                &quot;isMultiKey&quot; : false,</span><br><span class="line">                                &quot;multiKeyPaths&quot; : &#123;</span><br><span class="line">                                        &quot;last_name&quot; : [ ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;isUnique&quot; : false,</span><br><span class="line">                                &quot;isSparse&quot; : false,</span><br><span class="line">                                &quot;isPartial&quot; : false,</span><br><span class="line">                                &quot;indexVersion&quot; : 2,</span><br><span class="line">                                &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                                &quot;indexBounds&quot; : &#123;</span><br><span class="line">                                        &quot;last_name&quot; : [</span><br><span class="line">                                                &quot;[\&quot;Johnson\&quot;, \&quot;Johnson\&quot;]&quot;</span><br><span class="line">                                        ]</span><br><span class="line">                                &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;rejectedPlans&quot; : [ ]</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;executionStats&quot; : &#123;</span><br><span class="line">                &quot;executionSuccess&quot; : true,</span><br><span class="line">                &quot;nReturned&quot; : 794,</span><br><span class="line">                &quot;executionTimeMillis&quot; : 28,</span><br><span class="line">                &quot;totalKeysExamined&quot; : 794,</span><br><span class="line">                &quot;totalDocsExamined&quot; : 794,</span><br><span class="line">                &quot;executionStages&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;FETCH&quot;,</span><br><span class="line">                        &quot;nReturned&quot; : 794,</span><br><span class="line">                        &quot;executionTimeMillisEstimate&quot; : 0,</span><br><span class="line">                        &quot;works&quot; : 795,</span><br><span class="line">                        &quot;advanced&quot; : 794,</span><br><span class="line">                        &quot;needTime&quot; : 0,</span><br><span class="line">                        &quot;needYield&quot; : 0,</span><br><span class="line">                        &quot;saveState&quot; : 6,</span><br><span class="line">                        &quot;restoreState&quot; : 6,</span><br><span class="line">                        &quot;isEOF&quot; : 1,</span><br><span class="line">                        &quot;invalidates&quot; : 0,</span><br><span class="line">                        &quot;docsExamined&quot; : 794,</span><br><span class="line">                        &quot;alreadyHasObj&quot; : 0,</span><br><span class="line">                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                &quot;stage&quot; : &quot;IXSCAN&quot;,</span><br><span class="line">                                &quot;nReturned&quot; : 794,</span><br><span class="line">                                &quot;executionTimeMillisEstimate&quot; : 0,</span><br><span class="line">                                &quot;works&quot; : 795,</span><br><span class="line">                                &quot;advanced&quot; : 794,</span><br><span class="line">                                &quot;needTime&quot; : 0,</span><br><span class="line">                                &quot;needYield&quot; : 0,</span><br><span class="line">                                &quot;saveState&quot; : 6,</span><br><span class="line">                                &quot;restoreState&quot; : 6,</span><br><span class="line">                                &quot;isEOF&quot; : 1,</span><br><span class="line">                                &quot;invalidates&quot; : 0,</span><br><span class="line">                                &quot;keyPattern&quot; : &#123;</span><br><span class="line">                                        &quot;last_name&quot; : 1</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;indexName&quot; : &quot;last_name_1&quot;,</span><br><span class="line">                                &quot;isMultiKey&quot; : false,</span><br><span class="line">                                &quot;multiKeyPaths&quot; : &#123;</span><br><span class="line">                                        &quot;last_name&quot; : [ ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;isUnique&quot; : false,</span><br><span class="line">                                &quot;isSparse&quot; : false,</span><br><span class="line">                                &quot;isPartial&quot; : false,</span><br><span class="line">                                &quot;indexVersion&quot; : 2,</span><br><span class="line">                                &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                                &quot;indexBounds&quot; : &#123;</span><br><span class="line">                                        &quot;last_name&quot; : [</span><br><span class="line">                                                &quot;[\&quot;Johnson\&quot;, \&quot;Johnson\&quot;]&quot;</span><br><span class="line">                                        ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;keysExamined&quot; : 794,</span><br><span class="line">                                &quot;seeks&quot; : 1,</span><br><span class="line">                                &quot;dupsTested&quot; : 0,</span><br><span class="line">                                &quot;dupsDropped&quot; : 0,</span><br><span class="line">                                &quot;seenInvalidated&quot; : 0</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;serverInfo&quot; : &#123;</span><br><span class="line">                &quot;host&quot; : &quot;YZJC1606-0218&quot;,</span><br><span class="line">                &quot;port&quot; : 27017,</span><br><span class="line">                &quot;version&quot; : &quot;3.4.7&quot;,</span><br><span class="line">                &quot;gitVersion&quot; : &quot;cf38c1b8a0a8dca4a11737581beafef4fe120bcd&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;ok&quot; : 1</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong><em>从上面的分析看到，我们扫描了794  个文档，扫描了794个索引(executionStats.TotalKeyExamined)   (executionStats.totalDocsExamined，查询到了794（executionStats.nReturned）条名字Johnson的人员信息，查询总共共花费28ms(executionStats.executionTimeMillisEstimate)</em></strong><br><strong><em>采用的策略是FETCH(executionStats.executionStats.stage) 根据索引进行匹配用时为0ms（executionStats.executionStats.executionTimeMillisEstimate）;<br>采用索引进行扫描 IXSCAN（executionStats.executionStats.inputStage.stage）,扫描共花费时间0ms  (executionStats.executionStats.inputStage.executionTimeMillisEstimate)</em></strong></p>
<p>通过这个分析，我们可以看到效率得到改善。并且对比两个分析结果，发现，如果没有使用索引，则结果中没有executionStats.executionStats.inputStage这个内容输出。<br>我们大致可以得到分析结果的含义：<br>executionStats：查询整个状态<br>executionStats.executionStats：查询返回结果的状态<br>executionStats.executionStats.inputStage：扫描的状态（查询没有使用索引则没有该项返回值）</p>
<p>有时候我们可能要根据多个条件来查询人员信息，例如根据姓，名，和email，这样就必须要用到复合查询，那么我们来看下 <em>单索引对复合查询</em> 的影响</p>
<h6 id="只有一个单索引-last-name"><a href="#只有一个单索引-last-name" class="headerlink" title="只有一个单索引 last_name"></a>只有一个单索引 last_name</h6><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&gt; explain.find(&#123;&quot;first_name&quot;:&quot;Martin&quot;, &quot;last_name&quot;:&quot;Johnson&quot;, &quot;email&quot;:&quot;sboyd@yahoo.com&quot;&#125;)</span><br><span class="line">&#123;</span><br><span class="line">        &quot;queryPlanner&quot; : &#123;</span><br><span class="line">                &quot;plannerVersion&quot; : 1,</span><br><span class="line">                &quot;namespace&quot; : &quot;m201.people&quot;,</span><br><span class="line">                &quot;indexFilterSet&quot; : false,</span><br><span class="line">                &quot;parsedQuery&quot; : &#123;</span><br><span class="line">                        &quot;$and&quot; : [</span><br><span class="line">                                &#123;</span><br><span class="line">                                        &quot;email&quot; : &#123;</span><br><span class="line">                                                &quot;$eq&quot; : &quot;sboyd@yahoo.com&quot;</span><br><span class="line">                                        &#125;</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &#123;</span><br><span class="line">                                        &quot;first_name&quot; : &#123;</span><br><span class="line">                                                &quot;$eq&quot; : &quot;Martin&quot;</span><br><span class="line">                                        &#125;</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &#123;</span><br><span class="line">                                        &quot;last_name&quot; : &#123;</span><br><span class="line">                                                &quot;$eq&quot; : &quot;Johnson&quot;</span><br><span class="line">                                        &#125;</span><br><span class="line">                                &#125;</span><br><span class="line">                        ]</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;winningPlan&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;FETCH&quot;,</span><br><span class="line">                        &quot;filter&quot; : &#123;</span><br><span class="line">                                &quot;$and&quot; : [</span><br><span class="line">                                        &#123;</span><br><span class="line">                                                &quot;email&quot; : &#123;</span><br><span class="line">                                                        &quot;$eq&quot; : &quot;sboyd@yahoo.com&quot;</span><br><span class="line">                                                &#125;</span><br><span class="line">                                        &#125;,</span><br><span class="line">                                        &#123;</span><br><span class="line">                                                &quot;first_name&quot; : &#123;</span><br><span class="line">                                                        &quot;$eq&quot; : &quot;Martin&quot;</span><br><span class="line">                                                &#125;</span><br><span class="line">                                        &#125;</span><br><span class="line">                                ]</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                &quot;stage&quot; : &quot;IXSCAN&quot;,</span><br><span class="line">                                &quot;keyPattern&quot; : &#123;</span><br><span class="line">                                        &quot;last_name&quot; : 1</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;indexName&quot; : &quot;last_name_1&quot;,</span><br><span class="line">                                &quot;isMultiKey&quot; : false,</span><br><span class="line">                                &quot;multiKeyPaths&quot; : &#123;</span><br><span class="line">                                        &quot;last_name&quot; : [ ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;isUnique&quot; : false,</span><br><span class="line">                                &quot;isSparse&quot; : false,</span><br><span class="line">                                &quot;isPartial&quot; : false,</span><br><span class="line">                                &quot;indexVersion&quot; : 2,</span><br><span class="line">                                &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                                &quot;indexBounds&quot; : &#123;</span><br><span class="line">                                        &quot;last_name&quot; : [</span><br><span class="line">                                                &quot;[\&quot;Johnson\&quot;, \&quot;Johnson\&quot;]&quot;</span><br><span class="line">                                        ]</span><br><span class="line">                                &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;rejectedPlans&quot; : [ ]</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;executionStats&quot; : &#123;</span><br><span class="line">                &quot;executionSuccess&quot; : true,</span><br><span class="line">                &quot;nReturned&quot; : 1,</span><br><span class="line">                &quot;executionTimeMillis&quot; : 1,</span><br><span class="line">                &quot;totalKeysExamined&quot; : 794,</span><br><span class="line">                &quot;totalDocsExamined&quot; : 794,</span><br><span class="line">                &quot;executionStages&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;FETCH&quot;,</span><br><span class="line">                        &quot;filter&quot; : &#123;</span><br><span class="line">                                &quot;$and&quot; : [</span><br><span class="line">                                        &#123;</span><br><span class="line">                                                &quot;email&quot; : &#123;</span><br><span class="line">                                                        &quot;$eq&quot; : &quot;sboyd@yahoo.com&quot;</span><br><span class="line">                                                &#125;</span><br><span class="line">                                        &#125;,</span><br><span class="line">                                        &#123;</span><br><span class="line">                                                &quot;first_name&quot; : &#123;</span><br><span class="line">                                                        &quot;$eq&quot; : &quot;Martin&quot;</span><br><span class="line">                                                &#125;</span><br><span class="line">                                        &#125;</span><br><span class="line">                                ]</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;nReturned&quot; : 1,</span><br><span class="line">                        &quot;executionTimeMillisEstimate&quot; : 0,</span><br><span class="line">                        &quot;works&quot; : 795,</span><br><span class="line">                        &quot;advanced&quot; : 1,</span><br><span class="line">                        &quot;needTime&quot; : 793,</span><br><span class="line">                        &quot;needYield&quot; : 0,</span><br><span class="line">                        &quot;saveState&quot; : 6,</span><br><span class="line">                        &quot;restoreState&quot; : 6,</span><br><span class="line">                        &quot;isEOF&quot; : 1,</span><br><span class="line">                        &quot;invalidates&quot; : 0,</span><br><span class="line">                        &quot;docsExamined&quot; : 794,</span><br><span class="line">                        &quot;alreadyHasObj&quot; : 0,</span><br><span class="line">                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                &quot;stage&quot; : &quot;IXSCAN&quot;,</span><br><span class="line">                                &quot;nReturned&quot; : 794,</span><br><span class="line">                                &quot;executionTimeMillisEstimate&quot; : 0,</span><br><span class="line">                                &quot;works&quot; : 795,</span><br><span class="line">                                &quot;advanced&quot; : 794,</span><br><span class="line">                                &quot;needTime&quot; : 0,</span><br><span class="line">                                &quot;needYield&quot; : 0,</span><br><span class="line">                                &quot;saveState&quot; : 6,</span><br><span class="line">                                &quot;restoreState&quot; : 6,</span><br><span class="line">                                &quot;isEOF&quot; : 1,</span><br><span class="line">                                &quot;invalidates&quot; : 0,</span><br><span class="line">                                &quot;keyPattern&quot; : &#123;</span><br><span class="line">                                        &quot;last_name&quot; : 1</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;indexName&quot; : &quot;last_name_1&quot;,</span><br><span class="line">                                &quot;isMultiKey&quot; : false,</span><br><span class="line">                                &quot;multiKeyPaths&quot; : &#123;</span><br><span class="line">                                        &quot;last_name&quot; : [ ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;isUnique&quot; : false,</span><br><span class="line">                                &quot;isSparse&quot; : false,</span><br><span class="line">                                &quot;isPartial&quot; : false,</span><br><span class="line">                                &quot;indexVersion&quot; : 2,</span><br><span class="line">                                &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                                &quot;indexBounds&quot; : &#123;</span><br><span class="line">                                        &quot;last_name&quot; : [</span><br><span class="line">                                                &quot;[\&quot;Johnson\&quot;, \&quot;Johnson\&quot;]&quot;</span><br><span class="line">                                        ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;keysExamined&quot; : 794,</span><br><span class="line">                                &quot;seeks&quot; : 1,</span><br><span class="line">                                &quot;dupsTested&quot; : 0,</span><br><span class="line">                                &quot;dupsDropped&quot; : 0,</span><br><span class="line">                                &quot;seenInvalidated&quot; : 0</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;serverInfo&quot; : &#123;</span><br><span class="line">                &quot;host&quot; : &quot;YZJC1606-0218&quot;,</span><br><span class="line">                &quot;port&quot; : 27017,</span><br><span class="line">                &quot;version&quot; : &quot;3.4.7&quot;,</span><br><span class="line">                &quot;gitVersion&quot; : &quot;cf38c1b8a0a8dca4a11737581beafef4fe120bcd&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;ok&quot; : 1</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>虽然我们没有建立first_name_last_name_email这样的复合索引，但是通过查询计划的分析，我们发现，查询仍然使用了索引，看下如下的分析：</p>
<ul>
<li>查询总共用时(executionStats.totalexecutionTimeMillis)：1ms、共返回结果（executionStats.nReturned）：1个、扫描文档(executionStats.totalDocsExamined)：794、扫描索引（executionStats.totalKeysExamined）：794个。</li>
<li>查询匹配的策略为（executionStats.executionStats.stage）：FETCH、过滤条件为(executionStats.executionStats.filter)：email,first_name。这一点很奇怪，我们明明根据first_name_last_name_email三个条件来查询的，为什么filter只有两个了呢？我们再看下面的内容</li>
<li>在查询策略下，搜索策略为(executionStats.executionStats.inputStage.stage)：IXSCAN、我们先来看索引的键是(executionStats.executionStats.inputStage.indexName)：last_name_1、索引用时(executionStats.executionStats.inputStage.totalexecutionTimeMillis)：0ms、返回文档（executionStats.executionStats.inputStage.workds）：794个，使用了工作分片（executionStats.executionStats.inputStage.nReturned）：795个（比返回文档多一个！）</li>
</ul>
<p><strong><em>我们发现，在单独使用last_name条件，使用索引查询时，分析的状况也是一样，由此我们可以总结Mongo索引的一条规则：    </em></strong></p>
<font color="FireBrick" size="12">  如果Mongo复合查询的条件中，有一个条件上已经创建了单索引，那么查询时，Mongo引擎会帮我们优化，先根据有索引的字段进行扫描查询（IXSCAN），然后将产生的返回值与其他的条件进行匹配（FETCH）返回最终结果。<br>那么我们自然又提出疑问，如果我们有多个单个索引，在复和查询中用到了，会不会都会使用索引呢，我们在first_name上加一个索引再进行上述复合查询实验一下。</font><br>###### 有多个单索引 last_name、first_name<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&gt; db.people.createIndex(&#123;first_name:1&#125;)</span><br><span class="line">&#123;</span><br><span class="line">        &quot;createdCollectionAutomatically&quot; : false,</span><br><span class="line">        &quot;numIndexesBefore&quot; : 2,</span><br><span class="line">        &quot;numIndexesAfter&quot; : 3,</span><br><span class="line">        &quot;ok&quot; : 1</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&gt; explain.find(&#123;&quot;first_name&quot;:&quot;Martin&quot;, &quot;last_name&quot;:&quot;Johnson&quot;, &quot;email&quot;:&quot;sboyd@yahoo.com&quot;&#125;)</span><br><span class="line">&#123;</span><br><span class="line">        &quot;queryPlanner&quot; : &#123;</span><br><span class="line">                &quot;plannerVersion&quot; : 1,</span><br><span class="line">                &quot;namespace&quot; : &quot;m201.people&quot;,</span><br><span class="line">                &quot;indexFilterSet&quot; : false,</span><br><span class="line">                &quot;parsedQuery&quot; : &#123;</span><br><span class="line">                        &quot;$and&quot; : [</span><br><span class="line">                                &#123;</span><br><span class="line">                                        &quot;email&quot; : &#123;</span><br><span class="line">                                                &quot;$eq&quot; : &quot;sboyd@yahoo.com&quot;</span><br><span class="line">                                        &#125;</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &#123;</span><br><span class="line">                                        &quot;first_name&quot; : &#123;</span><br><span class="line">                                                &quot;$eq&quot; : &quot;Martin&quot;</span><br><span class="line">                                        &#125;</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &#123;</span><br><span class="line">                                        &quot;last_name&quot; : &#123;</span><br><span class="line">                                                &quot;$eq&quot; : &quot;Johnson&quot;</span><br><span class="line">                                        &#125;</span><br><span class="line">                                &#125;</span><br><span class="line">                        ]</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;winningPlan&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;FETCH&quot;,</span><br><span class="line">                        &quot;filter&quot; : &#123;</span><br><span class="line">                                &quot;$and&quot; : [</span><br><span class="line">                                        &#123;</span><br><span class="line">                                                &quot;email&quot; : &#123;</span><br><span class="line">                                                        &quot;$eq&quot; : &quot;sboyd@yahoo.com&quot;</span><br><span class="line">                                                &#125;</span><br><span class="line">                                        &#125;,</span><br><span class="line">                                        &#123;</span><br><span class="line">                                                &quot;last_name&quot; : &#123;</span><br><span class="line">                                                        &quot;$eq&quot; : &quot;Johnson&quot;</span><br><span class="line">                                                &#125;</span><br><span class="line">                                        &#125;</span><br><span class="line">                                ]</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                &quot;stage&quot; : &quot;IXSCAN&quot;,</span><br><span class="line">                                &quot;keyPattern&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : 1</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;indexName&quot; : &quot;first_name_1&quot;,</span><br><span class="line">                                &quot;isMultiKey&quot; : false,</span><br><span class="line">                                &quot;multiKeyPaths&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : [ ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;isUnique&quot; : false,</span><br><span class="line">                                &quot;isSparse&quot; : false,</span><br><span class="line">                                &quot;isPartial&quot; : false,</span><br><span class="line">                                &quot;indexVersion&quot; : 2,</span><br><span class="line">                                &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                                &quot;indexBounds&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : [</span><br><span class="line">                                                &quot;[\&quot;Martin\&quot;, \&quot;Martin\&quot;]&quot;</span><br><span class="line">                                        ]</span><br><span class="line">                                &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;rejectedPlans&quot; : [</span><br><span class="line">                        &#123;</span><br><span class="line">                                &quot;stage&quot; : &quot;FETCH&quot;,</span><br><span class="line">                                &quot;filter&quot; : &#123;</span><br><span class="line">                                        &quot;$and&quot; : [</span><br><span class="line">                                                &#123;</span><br><span class="line">                                                        &quot;email&quot; : &#123;</span><br><span class="line">                                                                &quot;$eq&quot; : &quot;sboyd@yahoo.com&quot;</span><br><span class="line">                                                        &#125;</span><br><span class="line">                                                &#125;,</span><br><span class="line">                                                &#123;</span><br><span class="line">                                                        &quot;first_name&quot; : &#123;</span><br><span class="line">                                                                &quot;$eq&quot; : &quot;Martin&quot;</span><br><span class="line">                                                        &#125;</span><br><span class="line">                                                &#125;</span><br><span class="line">                                        ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;inputStage&quot; : &#123;</span><br><span class="line">                                        &quot;stage&quot; : &quot;IXSCAN&quot;,</span><br><span class="line">                                        &quot;keyPattern&quot; : &#123;</span><br><span class="line">                                                &quot;last_name&quot; : 1</span><br><span class="line">                                        &#125;,</span><br><span class="line">                                        &quot;indexName&quot; : &quot;last_name_1&quot;,</span><br><span class="line">                                        &quot;isMultiKey&quot; : false,</span><br><span class="line">                                        &quot;multiKeyPaths&quot; : &#123;</span><br><span class="line">                                                &quot;last_name&quot; : [ ]</span><br><span class="line">                                        &#125;,</span><br><span class="line">                                        &quot;isUnique&quot; : false,</span><br><span class="line">                                        &quot;isSparse&quot; : false,</span><br><span class="line">                                        &quot;isPartial&quot; : false,</span><br><span class="line">                                        &quot;indexVersion&quot; : 2,</span><br><span class="line">                                        &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                                        &quot;indexBounds&quot; : &#123;</span><br><span class="line">                                                &quot;last_name&quot; : [</span><br><span class="line">                                                        &quot;[\&quot;Johnson\&quot;, \&quot;Johnson\&quot;]&quot;</span><br><span class="line">                                                ]</span><br><span class="line">                                        &#125;</span><br><span class="line">                                &#125;</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &#123;</span><br><span class="line">                                &quot;stage&quot; : &quot;FETCH&quot;,</span><br><span class="line">                                &quot;filter&quot; : &#123;</span><br><span class="line">                                        &quot;$and&quot; : [</span><br><span class="line">                                                &#123;</span><br><span class="line">                                                        &quot;last_name&quot; : &#123;</span><br><span class="line">                                                                &quot;$eq&quot; : &quot;Johnson&quot;</span><br><span class="line">                                                        &#125;</span><br><span class="line">                                                &#125;,</span><br><span class="line">                                                &#123;</span><br><span class="line">                                                        &quot;first_name&quot; : &#123;</span><br><span class="line">                                                                &quot;$eq&quot; : &quot;Martin&quot;</span><br><span class="line">                                                        &#125;</span><br><span class="line">                                                &#125;,</span><br><span class="line">                                                &#123;</span><br><span class="line">                                                        &quot;email&quot; : &#123;</span><br><span class="line">                                                                &quot;$eq&quot; : &quot;sboyd@yahoo.com&quot;</span><br><span class="line">                                                        &#125;</span><br><span class="line">                                                &#125;</span><br><span class="line">                                        ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;inputStage&quot; : &#123;</span><br><span class="line">                                        &quot;stage&quot; : &quot;AND_SORTED&quot;,</span><br><span class="line">                                        &quot;inputStages&quot; : [</span><br><span class="line">                                                &#123;</span><br><span class="line">                                                        &quot;stage&quot; : &quot;IXSCAN&quot;,</span><br><span class="line">                                                        &quot;keyPattern&quot; : &#123;</span><br><span class="line">                                                                &quot;last_name&quot; : 1</span><br><span class="line">                                                        &#125;,</span><br><span class="line">                                                        &quot;indexName&quot; : &quot;last_name_1&quot;,</span><br><span class="line">                                                        &quot;isMultiKey&quot; : false,</span><br><span class="line">                                                        &quot;multiKeyPaths&quot; : &#123;</span><br><span class="line">                                                                &quot;last_name&quot; : [ ]</span><br><span class="line">                                                        &#125;,</span><br><span class="line">                                                        &quot;isUnique&quot; : false,</span><br><span class="line">                                                        &quot;isSparse&quot; : false,</span><br><span class="line">                                                        &quot;isPartial&quot; : false,</span><br><span class="line">                                                        &quot;indexVersion&quot; : 2,</span><br><span class="line">                                                        &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                                                        &quot;indexBounds&quot; : &#123;</span><br><span class="line">                                                                &quot;last_name&quot; : [</span><br><span class="line">                                                                        &quot;[\&quot;Johnson\&quot;, \&quot;Johnson\&quot;]&quot;</span><br><span class="line">                                                                ]</span><br><span class="line">                                                        &#125;</span><br><span class="line">                                                &#125;,</span><br><span class="line">                                                &#123;</span><br><span class="line">                                                        &quot;stage&quot; : &quot;IXSCAN&quot;,</span><br><span class="line">                                                        &quot;keyPattern&quot; : &#123;</span><br><span class="line">                                                                &quot;first_name&quot; : 1</span><br><span class="line">                                                        &#125;,</span><br><span class="line">                                                        &quot;indexName&quot; : &quot;first_name_1&quot;,</span><br><span class="line">                                                        &quot;isMultiKey&quot; : false,</span><br><span class="line">                                                        &quot;multiKeyPaths&quot; : &#123;</span><br><span class="line">                                                                &quot;first_name&quot; : [ ]</span><br><span class="line">                                                        &#125;,</span><br><span class="line">                                                        &quot;isUnique&quot; : false,</span><br><span class="line">                                                        &quot;isSparse&quot; : false,</span><br><span class="line">                                                        &quot;isPartial&quot; : false,</span><br><span class="line">                                                        &quot;indexVersion&quot; : 2,</span><br><span class="line">                                                        &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                                                        &quot;indexBounds&quot; : &#123;</span><br><span class="line">                                                                &quot;first_name&quot; : [</span><br><span class="line">                                                                        &quot;[\&quot;Martin\&quot;, \&quot;Martin\&quot;]&quot;</span><br><span class="line">                                                                ]</span><br><span class="line">                                                        &#125;</span><br><span class="line">                                                &#125;</span><br><span class="line">                                        ]</span><br><span class="line">                                &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                ]</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;executionStats&quot; : &#123;</span><br><span class="line">                &quot;executionSuccess&quot; : true,</span><br><span class="line">                &quot;nReturned&quot; : 1,</span><br><span class="line">                &quot;executionTimeMillis&quot; : 58,</span><br><span class="line">                &quot;totalKeysExamined&quot; : 58,</span><br><span class="line">                &quot;totalDocsExamined&quot; : 58,</span><br><span class="line">                &quot;executionStages&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;FETCH&quot;,</span><br><span class="line">                        &quot;filter&quot; : &#123;</span><br><span class="line">                                &quot;$and&quot; : [</span><br><span class="line">                                        &#123;</span><br><span class="line">                                                &quot;email&quot; : &#123;</span><br><span class="line">                                                        &quot;$eq&quot; : &quot;sboyd@yahoo.com&quot;</span><br><span class="line">                                                &#125;</span><br><span class="line">                                        &#125;,</span><br><span class="line">                                        &#123;</span><br><span class="line">                                                &quot;last_name&quot; : &#123;</span><br><span class="line">                                                        &quot;$eq&quot; : &quot;Johnson&quot;</span><br><span class="line">                                                &#125;</span><br><span class="line">                                        &#125;</span><br><span class="line">                                ]</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;nReturned&quot; : 1,</span><br><span class="line">                        &quot;executionTimeMillisEstimate&quot; : 0,</span><br><span class="line">                        &quot;works&quot; : 60,</span><br><span class="line">                        &quot;advanced&quot; : 1,</span><br><span class="line">                        &quot;needTime&quot; : 57,</span><br><span class="line">                        &quot;needYield&quot; : 0,</span><br><span class="line">                        &quot;saveState&quot; : 2,</span><br><span class="line">                        &quot;restoreState&quot; : 2,</span><br><span class="line">                        &quot;isEOF&quot; : 1,</span><br><span class="line">                        &quot;invalidates&quot; : 0,</span><br><span class="line">                        &quot;docsExamined&quot; : 58,</span><br><span class="line">                        &quot;alreadyHasObj&quot; : 0,</span><br><span class="line">                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                &quot;stage&quot; : &quot;IXSCAN&quot;,</span><br><span class="line">                                &quot;nReturned&quot; : 58,</span><br><span class="line">                                &quot;executionTimeMillisEstimate&quot; : 0,</span><br><span class="line">                                &quot;works&quot; : 59,</span><br><span class="line">                                &quot;advanced&quot; : 58,</span><br><span class="line">                                &quot;needTime&quot; : 0,</span><br><span class="line">                                &quot;needYield&quot; : 0,</span><br><span class="line">                                &quot;saveState&quot; : 2,</span><br><span class="line">                                &quot;restoreState&quot; : 2,</span><br><span class="line">                                &quot;isEOF&quot; : 1,</span><br><span class="line">                                &quot;invalidates&quot; : 0,</span><br><span class="line">                                &quot;keyPattern&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : 1</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;indexName&quot; : &quot;first_name_1&quot;,</span><br><span class="line">                                &quot;isMultiKey&quot; : false,</span><br><span class="line">                                &quot;multiKeyPaths&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : [ ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;isUnique&quot; : false,</span><br><span class="line">                                &quot;isSparse&quot; : false,</span><br><span class="line">                                &quot;isPartial&quot; : false,</span><br><span class="line">                                &quot;indexVersion&quot; : 2,</span><br><span class="line">                                &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                                &quot;indexBounds&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : [</span><br><span class="line">                                                &quot;[\&quot;Martin\&quot;, \&quot;Martin\&quot;]&quot;</span><br><span class="line">                                        ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;keysExamined&quot; : 58,</span><br><span class="line">                                &quot;seeks&quot; : 1,</span><br><span class="line">                                &quot;dupsTested&quot; : 0,</span><br><span class="line">                                &quot;dupsDropped&quot; : 0,</span><br><span class="line">                                &quot;seenInvalidated&quot; : 0</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;serverInfo&quot; : &#123;</span><br><span class="line">                &quot;host&quot; : &quot;YZJC1606-0218&quot;,</span><br><span class="line">                &quot;port&quot; : 27017,</span><br><span class="line">                &quot;version&quot; : &quot;3.4.7&quot;,</span><br><span class="line">                &quot;gitVersion&quot; : &quot;cf38c1b8a0a8dca4a11737581beafef4fe120bcd&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;ok&quot; : 1</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><br><br>+ 在分析executionStats之前，我们看下queryPlanner，这里除了之前的winningPlan之外，还多了一个返回项rejectedPlans，这两个的含义为Mongo引擎最后使用的查询计划，和拒绝了的查询计划。我们看到这次使用的查询计划，并不是如我们之前所想，会使用first_name,和last_name两个单索引去搜索得到索引结果，然后再匹配email的条件返回最终结果，而是根据first_name单索引去搜素，得到索引结果，然后再根据last_name,和email的条件去匹配返回最终结果。<br>+ 所以查询分析和之前使用last_name单索引的情况类似。总共用时：58ms、共扫描了58个文档和58个fist_name索引（？IXSCAN）（全部使用到了索引）。然后再根据email，last_name的条件去匹配（FETCH）索引的结果得到返回值。<br>+ 之前使用last_name的所以时扫描的文档或索引数为794个，这也是为什么使用first_name这个索引的查询计划胜出的原因<br>+ 还有就是，为什么这次的查询计划更优，而用时却更多？（58:1），这与数据是否加载到内存有关。<br><strong>根据上面的结论，我们有发现了Mongo索引的第二条规则</strong><br><br><font color="FireBrick" size="12">Mongo复合查询时，<strong><em>最多使用单索引中的一个，</em></strong> 并且Mongo会优化索引选择，选择需要索引数少的那个单索引为最终的执行计划。</font>



<h2 id="复合索引"><a href="#复合索引" class="headerlink" title="复合索引"></a>复合索引</h2><p>看完单索引，我们来看下 <em>复合索引对复合查询，以及单键查询</em> 的 效率的影响</p>
<h6 id="没有索引进行-复合查询"><a href="#没有索引进行-复合查询" class="headerlink" title="没有索引进行 复合查询"></a>没有索引进行 复合查询</h6><p>先清空所有的单键索引、再根据姓，名，和email，进行复合查询。<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&gt; db.people.dropIndex(&quot;last_name_1&quot;)</span><br><span class="line">&#123; &quot;nIndexesWas&quot; : 3, &quot;ok&quot; : 1 &#125;</span><br><span class="line">&gt; db.people.dropIndex(&quot;first_name_1&quot;)</span><br><span class="line">&#123; &quot;nIndexesWas&quot; : 2, &quot;ok&quot; : 1 &#125;</span><br><span class="line">&gt; db.people.getIndexes()</span><br><span class="line">[</span><br><span class="line">        &#123;</span><br><span class="line">                &quot;v&quot; : 2,</span><br><span class="line">                &quot;key&quot; : &#123;</span><br><span class="line">                        &quot;_id&quot; : 1</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;name&quot; : &quot;_id_&quot;,</span><br><span class="line">                &quot;ns&quot; : &quot;m201.people&quot;</span><br><span class="line">        &#125;</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line">&gt; explain.find(&#123;&quot;first_name&quot;:&quot;Martin&quot;, &quot;last_name&quot;:&quot;Johnson&quot;, &quot;email&quot;:&quot;sboyd@yahoo.com&quot;&#125;)</span><br><span class="line">&#123;</span><br><span class="line">        &quot;queryPlanner&quot; : &#123;</span><br><span class="line">                &quot;plannerVersion&quot; : 1,</span><br><span class="line">                &quot;namespace&quot; : &quot;m201.people&quot;,</span><br><span class="line">                &quot;indexFilterSet&quot; : false,</span><br><span class="line">                &quot;parsedQuery&quot; : &#123;</span><br><span class="line">                        &quot;$and&quot; : [</span><br><span class="line">                                &#123;</span><br><span class="line">                                        &quot;email&quot; : &#123;</span><br><span class="line">                                                &quot;$eq&quot; : &quot;sboyd@yahoo.com&quot;</span><br><span class="line">                                        &#125;</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &#123;</span><br><span class="line">                                        &quot;first_name&quot; : &#123;</span><br><span class="line">                                                &quot;$eq&quot; : &quot;Martin&quot;</span><br><span class="line">                                        &#125;</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &#123;</span><br><span class="line">                                        &quot;last_name&quot; : &#123;</span><br><span class="line">                                                &quot;$eq&quot; : &quot;Johnson&quot;</span><br><span class="line">                                        &#125;</span><br><span class="line">                                &#125;</span><br><span class="line">                        ]</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;winningPlan&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;COLLSCAN&quot;,</span><br><span class="line">                        &quot;filter&quot; : &#123;</span><br><span class="line">                                &quot;$and&quot; : [</span><br><span class="line">                                        &#123;</span><br><span class="line">                                                &quot;email&quot; : &#123;</span><br><span class="line">                                                        &quot;$eq&quot; : &quot;sboyd@yahoo.com&quot;</span><br><span class="line">                                                &#125;</span><br><span class="line">                                        &#125;,</span><br><span class="line">                                        &#123;</span><br><span class="line">                                                &quot;first_name&quot; : &#123;</span><br><span class="line">                                                        &quot;$eq&quot; : &quot;Martin&quot;</span><br><span class="line">                                                &#125;</span><br><span class="line">                                        &#125;,</span><br><span class="line">                                        &#123;</span><br><span class="line">                                                &quot;last_name&quot; : &#123;</span><br><span class="line">                                                        &quot;$eq&quot; : &quot;Johnson&quot;</span><br><span class="line">                                                &#125;</span><br><span class="line">                                        &#125;</span><br><span class="line">                                ]</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;direction&quot; : &quot;forward&quot;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;rejectedPlans&quot; : [ ]</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;executionStats&quot; : &#123;</span><br><span class="line">                &quot;executionSuccess&quot; : true,</span><br><span class="line">                &quot;nReturned&quot; : 1,</span><br><span class="line">                &quot;executionTimeMillis&quot; : 26,</span><br><span class="line">                &quot;totalKeysExamined&quot; : 0,</span><br><span class="line">                &quot;totalDocsExamined&quot; : 50475,</span><br><span class="line">                &quot;executionStages&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;COLLSCAN&quot;,</span><br><span class="line">                        &quot;filter&quot; : &#123;</span><br><span class="line">                                &quot;$and&quot; : [</span><br><span class="line">                                        &#123;</span><br><span class="line">                                                &quot;email&quot; : &#123;</span><br><span class="line">                                                        &quot;$eq&quot; : &quot;sboyd@yahoo.com&quot;</span><br><span class="line">                                                &#125;</span><br><span class="line">                                        &#125;,</span><br><span class="line">                                        &#123;</span><br><span class="line">                                                &quot;first_name&quot; : &#123;</span><br><span class="line">                                                        &quot;$eq&quot; : &quot;Martin&quot;</span><br><span class="line">                                                &#125;</span><br><span class="line">                                        &#125;,</span><br><span class="line">                                        &#123;</span><br><span class="line">                                                &quot;last_name&quot; : &#123;</span><br><span class="line">                                                        &quot;$eq&quot; : &quot;Johnson&quot;</span><br><span class="line">                                                &#125;</span><br><span class="line">                                        &#125;</span><br><span class="line">                                ]</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;nReturned&quot; : 1,</span><br><span class="line">                        &quot;executionTimeMillisEstimate&quot; : 30,</span><br><span class="line">                        &quot;works&quot; : 50477,</span><br><span class="line">                        &quot;advanced&quot; : 1,</span><br><span class="line">                        &quot;needTime&quot; : 50475,</span><br><span class="line">                        &quot;needYield&quot; : 0,</span><br><span class="line">                        &quot;saveState&quot; : 394,</span><br><span class="line">                        &quot;restoreState&quot; : 394,</span><br><span class="line">                        &quot;isEOF&quot; : 1,</span><br><span class="line">                        &quot;invalidates&quot; : 0,</span><br><span class="line">                        &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                        &quot;docsExamined&quot; : 50475</span><br><span class="line">                &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;serverInfo&quot; : &#123;</span><br><span class="line">                &quot;host&quot; : &quot;YZJC1606-0218&quot;,</span><br><span class="line">                &quot;port&quot; : 27017,</span><br><span class="line">                &quot;version&quot; : &quot;3.4.7&quot;,</span><br><span class="line">                &quot;gitVersion&quot; : &quot;cf38c1b8a0a8dca4a11737581beafef4fe120bcd&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;ok&quot; : 1</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<ul>
<li>查询总共用时(executionStats.totalexecutionTimeMillis)：26ms、共返回结果（executionStats.nReturned）：1个、扫描文档(executionStats.totalDocsExamined)：50475（全表扫描）、扫描索引（executionStats.totalKeysExamined）：0个。</li>
<li>查询匹配的策略为（executionStats.executionStats.stage）：COLLSCAN。因为没有进行索引扫描，所以没有inputStage返回值。</li>
<li><strong><em>这个executionStats.executionStats.executionTimeMillisEstimate:30比executionStats.executionTimeMillis花费时间还长，这是怎么解释？ </em></strong><br>可以看到，我们进行了全表扫描，没有用到任何索引，扫描50475仅仅返回一条记录。效率很低。</li>
</ul>
<h6 id="添加复合索引-first-name-last-name-email进行查询"><a href="#添加复合索引-first-name-last-name-email进行查询" class="headerlink" title="添加复合索引 first_name_last_name_email进行查询"></a>添加复合索引 first_name_last_name_email进行查询</h6><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&gt; db.people.createIndex(&#123;&quot;first_name&quot;:1, &quot;last_name&quot;:1, &quot;email&quot;:1&#125;)</span><br><span class="line">&#123;</span><br><span class="line">        &quot;createdCollectionAutomatically&quot; : false,</span><br><span class="line">        &quot;numIndexesBefore&quot; : 1,</span><br><span class="line">        &quot;numIndexesAfter&quot; : 2,</span><br><span class="line">        &quot;ok&quot; : 1</span><br><span class="line">&#125;</span><br><span class="line">&gt; db.people.getIndexes()</span><br><span class="line">[</span><br><span class="line">        &#123;</span><br><span class="line">                &quot;v&quot; : 2,</span><br><span class="line">                &quot;key&quot; : &#123;</span><br><span class="line">                        &quot;_id&quot; : 1</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;name&quot; : &quot;_id_&quot;,</span><br><span class="line">                &quot;ns&quot; : &quot;m201.people&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">                &quot;v&quot; : 2,</span><br><span class="line">                &quot;key&quot; : &#123;</span><br><span class="line">                        &quot;first_name&quot; : 1,</span><br><span class="line">                        &quot;last_name&quot; : 1,</span><br><span class="line">                        &quot;email&quot; : 1</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;name&quot; : &quot;first_name_1_last_name_1_email_1&quot;,</span><br><span class="line">                &quot;ns&quot; : &quot;m201.people&quot;</span><br><span class="line">        &#125;</span><br><span class="line">]</span><br><span class="line">&gt; explain.find(&#123;&quot;first_name&quot;:&quot;Martin&quot;, &quot;last_name&quot;:&quot;Johnson&quot;, &quot;email&quot;:&quot;sboyd@yahoo.com&quot;&#125;)</span><br><span class="line">&#123;</span><br><span class="line">        &quot;queryPlanner&quot; : &#123;</span><br><span class="line">                &quot;plannerVersion&quot; : 1,</span><br><span class="line">                &quot;namespace&quot; : &quot;m201.people&quot;,</span><br><span class="line">                &quot;indexFilterSet&quot; : false,</span><br><span class="line">                &quot;parsedQuery&quot; : &#123;</span><br><span class="line">                        &quot;$and&quot; : [</span><br><span class="line">                                &#123;</span><br><span class="line">                                        &quot;email&quot; : &#123;</span><br><span class="line">                                                &quot;$eq&quot; : &quot;sboyd@yahoo.com&quot;</span><br><span class="line">                                        &#125;</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &#123;</span><br><span class="line">                                        &quot;first_name&quot; : &#123;</span><br><span class="line">                                                &quot;$eq&quot; : &quot;Martin&quot;</span><br><span class="line">                                        &#125;</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &#123;</span><br><span class="line">                                        &quot;last_name&quot; : &#123;</span><br><span class="line">                                                &quot;$eq&quot; : &quot;Johnson&quot;</span><br><span class="line">                                        &#125;</span><br><span class="line">                                &#125;</span><br><span class="line">                        ]</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;winningPlan&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;FETCH&quot;,</span><br><span class="line">                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                &quot;stage&quot; : &quot;IXSCAN&quot;,</span><br><span class="line">                                &quot;keyPattern&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : 1,</span><br><span class="line">                                        &quot;last_name&quot; : 1,</span><br><span class="line">                                        &quot;email&quot; : 1</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;indexName&quot; : &quot;first_name_1_last_name_1_email_1&quot;,</span><br><span class="line">                                &quot;isMultiKey&quot; : false,</span><br><span class="line">                                &quot;multiKeyPaths&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : [ ],</span><br><span class="line">                                        &quot;last_name&quot; : [ ],</span><br><span class="line">                                        &quot;email&quot; : [ ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;isUnique&quot; : false,</span><br><span class="line">                                &quot;isSparse&quot; : false,</span><br><span class="line">                                &quot;isPartial&quot; : false,</span><br><span class="line">                                &quot;indexVersion&quot; : 2,</span><br><span class="line">                                &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                                &quot;indexBounds&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : [</span><br><span class="line">                                                &quot;[\&quot;Martin\&quot;, \&quot;Martin\&quot;]&quot;</span><br><span class="line">                                        ],</span><br><span class="line">                                        &quot;last_name&quot; : [</span><br><span class="line">                                                &quot;[\&quot;Johnson\&quot;, \&quot;Johnson\&quot;]&quot;</span><br><span class="line">                                        ],</span><br><span class="line">                                        &quot;email&quot; : [</span><br><span class="line">                                                &quot;[\&quot;sboyd@yahoo.com\&quot;, \&quot;sboyd@yahoo.com\&quot;]&quot;</span><br><span class="line">                                        ]</span><br><span class="line">                                &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;rejectedPlans&quot; : [ ]</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;executionStats&quot; : &#123;</span><br><span class="line">                &quot;executionSuccess&quot; : true,</span><br><span class="line">                &quot;nReturned&quot; : 1,</span><br><span class="line">                &quot;executionTimeMillis&quot; : 1,</span><br><span class="line">                &quot;totalKeysExamined&quot; : 1,</span><br><span class="line">                &quot;totalDocsExamined&quot; : 1,</span><br><span class="line">                &quot;executionStages&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;FETCH&quot;,</span><br><span class="line">                        &quot;nReturned&quot; : 1,</span><br><span class="line">                        &quot;executionTimeMillisEstimate&quot; : 10,</span><br><span class="line">                        &quot;works&quot; : 2,</span><br><span class="line">                        &quot;advanced&quot; : 1,</span><br><span class="line">                        &quot;needTime&quot; : 0,</span><br><span class="line">                        &quot;needYield&quot; : 0,</span><br><span class="line">                        &quot;saveState&quot; : 0,</span><br><span class="line">                        &quot;restoreState&quot; : 0,</span><br><span class="line">                        &quot;isEOF&quot; : 1,</span><br><span class="line">                        &quot;invalidates&quot; : 0,</span><br><span class="line">                        &quot;docsExamined&quot; : 1,</span><br><span class="line">                        &quot;alreadyHasObj&quot; : 0,</span><br><span class="line">                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                &quot;stage&quot; : &quot;IXSCAN&quot;,</span><br><span class="line">                                &quot;nReturned&quot; : 1,</span><br><span class="line">                                &quot;executionTimeMillisEstimate&quot; : 10,</span><br><span class="line">                                &quot;works&quot; : 2,</span><br><span class="line">                                &quot;advanced&quot; : 1,</span><br><span class="line">                                &quot;needTime&quot; : 0,</span><br><span class="line">                                &quot;needYield&quot; : 0,</span><br><span class="line">                                &quot;saveState&quot; : 0,</span><br><span class="line">                                &quot;restoreState&quot; : 0,</span><br><span class="line">                                &quot;isEOF&quot; : 1,</span><br><span class="line">                                &quot;invalidates&quot; : 0,</span><br><span class="line">                                &quot;keyPattern&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : 1,</span><br><span class="line">                                        &quot;last_name&quot; : 1,</span><br><span class="line">                                        &quot;email&quot; : 1</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;indexName&quot; : &quot;first_name_1_last_name_1_email_1&quot;,</span><br><span class="line">                                &quot;isMultiKey&quot; : false,</span><br><span class="line">                                &quot;multiKeyPaths&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : [ ],</span><br><span class="line">                                        &quot;last_name&quot; : [ ],</span><br><span class="line">                                        &quot;email&quot; : [ ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;isUnique&quot; : false,</span><br><span class="line">                                &quot;isSparse&quot; : false,</span><br><span class="line">                                &quot;isPartial&quot; : false,</span><br><span class="line">                                &quot;indexVersion&quot; : 2,</span><br><span class="line">                                &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                                &quot;indexBounds&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : [</span><br><span class="line">                                                &quot;[\&quot;Martin\&quot;, \&quot;Martin\&quot;]&quot;</span><br><span class="line">                                        ],</span><br><span class="line">                                        &quot;last_name&quot; : [</span><br><span class="line">                                                &quot;[\&quot;Johnson\&quot;, \&quot;Johnson\&quot;]&quot;</span><br><span class="line">                                        ],</span><br><span class="line">                                        &quot;email&quot; : [</span><br><span class="line">                                                &quot;[\&quot;sboyd@yahoo.com\&quot;, \&quot;sboyd@yahoo.com\&quot;]&quot;</span><br><span class="line">                                        ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;keysExamined&quot; : 1,</span><br><span class="line">                                &quot;seeks&quot; : 1,</span><br><span class="line">                                &quot;dupsTested&quot; : 0,</span><br><span class="line">                                &quot;dupsDropped&quot; : 0,</span><br><span class="line">                                &quot;seenInvalidated&quot; : 0</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;serverInfo&quot; : &#123;</span><br><span class="line">                &quot;host&quot; : &quot;YZJC1606-0218&quot;,</span><br><span class="line">                &quot;port&quot; : 27017,</span><br><span class="line">                &quot;version&quot; : &quot;3.4.7&quot;,</span><br><span class="line">                &quot;gitVersion&quot; : &quot;cf38c1b8a0a8dca4a11737581beafef4fe120bcd&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;ok&quot; : 1</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>使用复合索引后。总共用时：1ms、共扫描了1个文档和1个索引（first_name_1_last_name_1_email_1）（IXSCAN）（全部使用到了索引）。我们看到，如果复合查询完全复合了复合索引，效率还是非常高的。</li>
</ul>
<h6 id="复合索引对单个条件查询的影响"><a href="#复合索引对单个条件查询的影响" class="headerlink" title="复合索引对单个条件查询的影响"></a>复合索引对单个条件查询的影响</h6><p>现在我们有复合索引first_name_1_last_name_1_email_1，那么这个索引会不会影响我们的单条件查询呢？我们也来实验一下。</p>
<p>根据first_name条件查询人员信心<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&gt; explain.find(&#123;first_name:&quot;Martin&quot;&#125;)</span><br><span class="line">&#123;</span><br><span class="line">        &quot;queryPlanner&quot; : &#123;</span><br><span class="line">                &quot;plannerVersion&quot; : 1,</span><br><span class="line">                &quot;namespace&quot; : &quot;m201.people&quot;,</span><br><span class="line">                &quot;indexFilterSet&quot; : false,</span><br><span class="line">                &quot;parsedQuery&quot; : &#123;</span><br><span class="line">                        &quot;first_name&quot; : &#123;</span><br><span class="line">                                &quot;$eq&quot; : &quot;Martin&quot;</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;winningPlan&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;FETCH&quot;,</span><br><span class="line">                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                &quot;stage&quot; : &quot;IXSCAN&quot;,</span><br><span class="line">                                &quot;keyPattern&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : 1,</span><br><span class="line">                                        &quot;last_name&quot; : 1,</span><br><span class="line">                                        &quot;email&quot; : 1</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;indexName&quot; : &quot;first_name_1_last_name_1_email_1&quot;,</span><br><span class="line">                                &quot;isMultiKey&quot; : false,</span><br><span class="line">                                &quot;multiKeyPaths&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : [ ],</span><br><span class="line">                                        &quot;last_name&quot; : [ ],</span><br><span class="line">                                        &quot;email&quot; : [ ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;isUnique&quot; : false,</span><br><span class="line">                                &quot;isSparse&quot; : false,</span><br><span class="line">                                &quot;isPartial&quot; : false,</span><br><span class="line">                                &quot;indexVersion&quot; : 2,</span><br><span class="line">                                &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                                &quot;indexBounds&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : [</span><br><span class="line">                                                &quot;[\&quot;Martin\&quot;, \&quot;Martin\&quot;]&quot;</span><br><span class="line">                                        ],</span><br><span class="line">                                        &quot;last_name&quot; : [</span><br><span class="line">                                                &quot;[MinKey, MaxKey]&quot;</span><br><span class="line">                                        ],</span><br><span class="line">                                        &quot;email&quot; : [</span><br><span class="line">                                                &quot;[MinKey, MaxKey]&quot;</span><br><span class="line">                                        ]</span><br><span class="line">                                &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;rejectedPlans&quot; : [ ]</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;executionStats&quot; : &#123;</span><br><span class="line">                &quot;executionSuccess&quot; : true,</span><br><span class="line">                &quot;nReturned&quot; : 58,</span><br><span class="line">                &quot;executionTimeMillis&quot; : 53,</span><br><span class="line">                &quot;totalKeysExamined&quot; : 58,</span><br><span class="line">                &quot;totalDocsExamined&quot; : 58,</span><br><span class="line">                &quot;executionStages&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;FETCH&quot;,</span><br><span class="line">                        &quot;nReturned&quot; : 58,</span><br><span class="line">                        &quot;executionTimeMillisEstimate&quot; : 30,</span><br><span class="line">                        &quot;works&quot; : 59,</span><br><span class="line">                        &quot;advanced&quot; : 58,</span><br><span class="line">                        &quot;needTime&quot; : 0,</span><br><span class="line">                        &quot;needYield&quot; : 0,</span><br><span class="line">                        &quot;saveState&quot; : 1,</span><br><span class="line">                        &quot;restoreState&quot; : 1,</span><br><span class="line">                        &quot;isEOF&quot; : 1,</span><br><span class="line">                        &quot;invalidates&quot; : 0,</span><br><span class="line">                        &quot;docsExamined&quot; : 58,</span><br><span class="line">                        &quot;alreadyHasObj&quot; : 0,</span><br><span class="line">                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                &quot;stage&quot; : &quot;IXSCAN&quot;,</span><br><span class="line">                                &quot;nReturned&quot; : 58,</span><br><span class="line">                                &quot;executionTimeMillisEstimate&quot; : 20,</span><br><span class="line">                                &quot;works&quot; : 59,</span><br><span class="line">                                &quot;advanced&quot; : 58,</span><br><span class="line">                                &quot;needTime&quot; : 0,</span><br><span class="line">                                &quot;needYield&quot; : 0,</span><br><span class="line">                                &quot;saveState&quot; : 1,</span><br><span class="line">                                &quot;restoreState&quot; : 1,</span><br><span class="line">                                &quot;isEOF&quot; : 1,</span><br><span class="line">                                &quot;invalidates&quot; : 0,</span><br><span class="line">                                &quot;keyPattern&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : 1,</span><br><span class="line">                                        &quot;last_name&quot; : 1,</span><br><span class="line">                                        &quot;email&quot; : 1</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;indexName&quot; : &quot;first_name_1_last_name_1_email_1&quot;,</span><br><span class="line">                                &quot;isMultiKey&quot; : false,</span><br><span class="line">                                &quot;multiKeyPaths&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : [ ],</span><br><span class="line">                                        &quot;last_name&quot; : [ ],</span><br><span class="line">                                        &quot;email&quot; : [ ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;isUnique&quot; : false,</span><br><span class="line">                                &quot;isSparse&quot; : false,</span><br><span class="line">                                &quot;isPartial&quot; : false,</span><br><span class="line">                                &quot;indexVersion&quot; : 2,</span><br><span class="line">                                &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                                &quot;indexBounds&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : [</span><br><span class="line">                                                &quot;[\&quot;Martin\&quot;, \&quot;Martin\&quot;]&quot;</span><br><span class="line">                                        ],</span><br><span class="line">                                        &quot;last_name&quot; : [</span><br><span class="line">                                                &quot;[MinKey, MaxKey]&quot;</span><br><span class="line">                                        ],</span><br><span class="line">                                        &quot;email&quot; : [</span><br><span class="line">                                                &quot;[MinKey, MaxKey]&quot;</span><br><span class="line">                                        ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;keysExamined&quot; : 58,</span><br><span class="line">                                &quot;seeks&quot; : 1,</span><br><span class="line">                                &quot;dupsTested&quot; : 0,</span><br><span class="line">                                &quot;dupsDropped&quot; : 0,</span><br><span class="line">                                &quot;seenInvalidated&quot; : 0</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;serverInfo&quot; : &#123;</span><br><span class="line">                &quot;host&quot; : &quot;YZJC1606-0218&quot;,</span><br><span class="line">                &quot;port&quot; : 27017,</span><br><span class="line">                &quot;version&quot; : &quot;3.4.7&quot;,</span><br><span class="line">                &quot;gitVersion&quot; : &quot;cf38c1b8a0a8dca4a11737581beafef4fe120bcd&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;ok&quot; : 1</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<ul>
<li>查询共用时53ms， 查询了58个文档，查询了58个索引(全部用到了索引)，返回了58个结果。执行状态为：IXSCAN，用到的索引为：first_name_1_last_name_1_email_1。</li>
<li>我们看到Mongo引擎先根据索引first_name_1_last_name_1_email_1索引到58个结果，然后FETCH了58个结果返回。<br>从查询分析结果看出，我们的单条件查询用到了我们的复合索引。</li>
</ul>
<p>根据last_name条件查询人员信息<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&gt; explain.find(&#123;last_name:&quot;Johnson&quot;&#125;)</span><br><span class="line">&#123;</span><br><span class="line">        &quot;queryPlanner&quot; : &#123;</span><br><span class="line">                &quot;plannerVersion&quot; : 1,</span><br><span class="line">                &quot;namespace&quot; : &quot;m201.people&quot;,</span><br><span class="line">                &quot;indexFilterSet&quot; : false,</span><br><span class="line">                &quot;parsedQuery&quot; : &#123;</span><br><span class="line">                        &quot;last_name&quot; : &#123;</span><br><span class="line">                                &quot;$eq&quot; : &quot;Johnson&quot;</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;winningPlan&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;COLLSCAN&quot;,</span><br><span class="line">                        &quot;filter&quot; : &#123;</span><br><span class="line">                                &quot;last_name&quot; : &#123;</span><br><span class="line">                                        &quot;$eq&quot; : &quot;Johnson&quot;</span><br><span class="line">                                &#125;</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;direction&quot; : &quot;forward&quot;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;rejectedPlans&quot; : [ ]</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;executionStats&quot; : &#123;</span><br><span class="line">                &quot;executionSuccess&quot; : true,</span><br><span class="line">                &quot;nReturned&quot; : 794,</span><br><span class="line">                &quot;executionTimeMillis&quot; : 95,</span><br><span class="line">                &quot;totalKeysExamined&quot; : 0,</span><br><span class="line">                &quot;totalDocsExamined&quot; : 50475,</span><br><span class="line">                &quot;executionStages&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;COLLSCAN&quot;,</span><br><span class="line">                        &quot;filter&quot; : &#123;</span><br><span class="line">                                &quot;last_name&quot; : &#123;</span><br><span class="line">                                        &quot;$eq&quot; : &quot;Johnson&quot;</span><br><span class="line">                                &#125;</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;nReturned&quot; : 794,</span><br><span class="line">                        &quot;executionTimeMillisEstimate&quot; : 20,</span><br><span class="line">                        &quot;works&quot; : 50477,</span><br><span class="line">                        &quot;advanced&quot; : 794,</span><br><span class="line">                        &quot;needTime&quot; : 49682,</span><br><span class="line">                        &quot;needYield&quot; : 0,</span><br><span class="line">                        &quot;saveState&quot; : 394,</span><br><span class="line">                        &quot;restoreState&quot; : 394,</span><br><span class="line">                        &quot;isEOF&quot; : 1,</span><br><span class="line">                        &quot;invalidates&quot; : 0,</span><br><span class="line">                        &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                        &quot;docsExamined&quot; : 50475</span><br><span class="line">                &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;serverInfo&quot; : &#123;</span><br><span class="line">                &quot;host&quot; : &quot;YZJC1606-0218&quot;,</span><br><span class="line">                &quot;port&quot; : 27017,</span><br><span class="line">                &quot;version&quot; : &quot;3.4.7&quot;,</span><br><span class="line">                &quot;gitVersion&quot; : &quot;cf38c1b8a0a8dca4a11737581beafef4fe120bcd&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;ok&quot; : 1</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<ul>
<li>查询共用时94ms， 查询了50475个文档（全文检索），查询了0个索引，返回了794个结果。执行状态为：COLLSCAN。<br>从这个查询分析来看，我们的复合索引first_name_1_last_name_1_email_1对查询没有任何影响。</li>
</ul>
<p>从上面两个查询分析我们得到另一个Mongo索引规则：<strong><em>前缀索引规则</em></strong></p>
<font color="FireBrick" size="12">  如果Mongo库中存在复合索引，那么Mongo索引的前缀索引可用在查询中。那么什么是前缀索引？例如first_name 、 first_name_last_name 都是first_name_1_last_name_1_email_1的前缀索引。 也就是说，按照复合索引顺序的子索引，都可以用在查询中。</font>

<h2 id="复合索引-前缀索引"><a href="#复合索引-前缀索引" class="headerlink" title="复合索引-前缀索引"></a>复合索引-前缀索引</h2><ul>
<li>复合索引从左边开始的前缀索引都能被使用*</li>
</ul>
<ul>
<li>如果有索引 a:1,b:1,c:1,d:1<br><em>根据前缀索引，如下查询都会匹配到索引</em><br>find({a:””,b:””,c:””,d:””})<br>find({a:””,b:””,c:””})<br>find({a:””,b:””})<br>find({a:””})</li>
</ul>
<p>如果是查询：find({a:””,b:””,d:””}) 只会使用到索引a:1,b:1,然后再去遍历通过a,b索引查询的所有文档找出和d相匹配的结果。（使用到了复合索引的部分索引，因为键索引的顺序无法满足前缀索引的条件，无法完全匹配到索引）</p>
<h2 id="加入排序后索引规则"><a href="#加入排序后索引规则" class="headerlink" title="加入排序后索引规则"></a>加入排序后索引规则</h2><p>我们有时候需要对我们查询的记录进行排序，更好的展现数据。例如，查询相同last_name的用户信息，并且对first_name进行排序，那么这样的查询会是怎样呢，我们分析一下<br>同样，我们通过单索引，和复合索引进行分析</p>
<h6 id="单索引对排序影响"><a href="#单索引对排序影响" class="headerlink" title="单索引对排序影响"></a>单索引对排序影响</h6><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">#只有last_name索引</span><br><span class="line">[</span><br><span class="line">        &#123;</span><br><span class="line">                &quot;v&quot; : 2,</span><br><span class="line">                &quot;key&quot; : &#123;</span><br><span class="line">                        &quot;_id&quot; : 1</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;name&quot; : &quot;_id_&quot;,</span><br><span class="line">                &quot;ns&quot; : &quot;m201.people&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">                &quot;v&quot; : 2,</span><br><span class="line">                &quot;key&quot; : &#123;</span><br><span class="line">                        &quot;last_name&quot; : 1</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;name&quot; : &quot;last_name_1&quot;,</span><br><span class="line">                &quot;ns&quot; : &quot;m201.people&quot;</span><br><span class="line">        &#125;</span><br><span class="line">]</span><br><span class="line">#执行计划分析</span><br><span class="line">&gt; explain.find(&#123;last_name:&quot;Johnson&quot;&#125;).sort(&#123;first_name:1&#125;)</span><br><span class="line">&#123;</span><br><span class="line">        &quot;queryPlanner&quot; : &#123;</span><br><span class="line">                &quot;plannerVersion&quot; : 1,</span><br><span class="line">                &quot;namespace&quot; : &quot;m201.people&quot;,</span><br><span class="line">                &quot;indexFilterSet&quot; : false,</span><br><span class="line">                &quot;parsedQuery&quot; : &#123;</span><br><span class="line">                        &quot;last_name&quot; : &#123;</span><br><span class="line">                                &quot;$eq&quot; : &quot;Johnson&quot;</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;winningPlan&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;SORT&quot;,</span><br><span class="line">                        &quot;sortPattern&quot; : &#123;</span><br><span class="line">                                &quot;first_name&quot; : 1</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                &quot;stage&quot; : &quot;SORT_KEY_GENERATOR&quot;,</span><br><span class="line">                                &quot;inputStage&quot; : &#123;</span><br><span class="line">                                        &quot;stage&quot; : &quot;FETCH&quot;,</span><br><span class="line">                                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                                &quot;stage&quot; : &quot;IXSCAN&quot;,</span><br><span class="line">                                                &quot;keyPattern&quot; : &#123;</span><br><span class="line">                                                        &quot;last_name&quot; : 1</span><br><span class="line">                                                &#125;,</span><br><span class="line">                                                &quot;indexName&quot; : &quot;last_name_1&quot;,</span><br><span class="line">                                                &quot;isMultiKey&quot; : false,</span><br><span class="line">                                                &quot;multiKeyPaths&quot; : &#123;</span><br><span class="line">                                                        &quot;last_name&quot; : [ ]</span><br><span class="line">                                                &#125;,</span><br><span class="line">                                                &quot;isUnique&quot; : false,</span><br><span class="line">                                                &quot;isSparse&quot; : false,</span><br><span class="line">                                                &quot;isPartial&quot; : false,</span><br><span class="line">                                                &quot;indexVersion&quot; : 2,</span><br><span class="line">                                                &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                                                &quot;indexBounds&quot; : &#123;</span><br><span class="line">                                                        &quot;last_name&quot; : [</span><br><span class="line">                                                                &quot;[\&quot;Johnson\&quot;, \&quot;Johnson\&quot;]&quot;</span><br><span class="line">                                                        ]</span><br><span class="line">                                                &#125;</span><br><span class="line">                                        &#125;</span><br><span class="line">                                &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;rejectedPlans&quot; : [ ]</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;executionStats&quot; : &#123;</span><br><span class="line">                &quot;executionSuccess&quot; : true,</span><br><span class="line">                &quot;nReturned&quot; : 794,</span><br><span class="line">                &quot;executionTimeMillis&quot; : 98,</span><br><span class="line">                &quot;totalKeysExamined&quot; : 794,</span><br><span class="line">                &quot;totalDocsExamined&quot; : 794,</span><br><span class="line">                &quot;executionStages&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;SORT&quot;,</span><br><span class="line">                        &quot;nReturned&quot; : 794,</span><br><span class="line">                        &quot;executionTimeMillisEstimate&quot; : 40,</span><br><span class="line">                        &quot;works&quot; : 1591,</span><br><span class="line">                        &quot;advanced&quot; : 794,</span><br><span class="line">                        &quot;needTime&quot; : 796,</span><br><span class="line">                        &quot;needYield&quot; : 0,</span><br><span class="line">                        &quot;saveState&quot; : 13,</span><br><span class="line">                        &quot;restoreState&quot; : 13,</span><br><span class="line">                        &quot;isEOF&quot; : 1,</span><br><span class="line">                        &quot;invalidates&quot; : 0,</span><br><span class="line">                        &quot;sortPattern&quot; : &#123;</span><br><span class="line">                                &quot;first_name&quot; : 1</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;memUsage&quot; : 314926,</span><br><span class="line">                        &quot;memLimit&quot; : 33554432,</span><br><span class="line">                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                &quot;stage&quot; : &quot;SORT_KEY_GENERATOR&quot;,</span><br><span class="line">                                &quot;nReturned&quot; : 794,</span><br><span class="line">                                &quot;executionTimeMillisEstimate&quot; : 20,</span><br><span class="line">                                &quot;works&quot; : 796,</span><br><span class="line">                                &quot;advanced&quot; : 794,</span><br><span class="line">                                &quot;needTime&quot; : 1,</span><br><span class="line">                                &quot;needYield&quot; : 0,</span><br><span class="line">                                &quot;saveState&quot; : 13,</span><br><span class="line">                                &quot;restoreState&quot; : 13,</span><br><span class="line">                                &quot;isEOF&quot; : 1,</span><br><span class="line">                                &quot;invalidates&quot; : 0,</span><br><span class="line">                                &quot;inputStage&quot; : &#123;</span><br><span class="line">                                        &quot;stage&quot; : &quot;FETCH&quot;,</span><br><span class="line">                                        &quot;nReturned&quot; : 794,</span><br><span class="line">                                        &quot;executionTimeMillisEstimate&quot; : 20,</span><br><span class="line">                                        &quot;works&quot; : 795,</span><br><span class="line">                                        &quot;advanced&quot; : 794,</span><br><span class="line">                                        &quot;needTime&quot; : 0,</span><br><span class="line">                                        &quot;needYield&quot; : 0,</span><br><span class="line">                                        &quot;saveState&quot; : 13,</span><br><span class="line">                                        &quot;restoreState&quot; : 13,</span><br><span class="line">                                        &quot;isEOF&quot; : 1,</span><br><span class="line">                                        &quot;invalidates&quot; : 0,</span><br><span class="line">                                        &quot;docsExamined&quot; : 794,</span><br><span class="line">                                        &quot;alreadyHasObj&quot; : 0,</span><br><span class="line">                                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                                &quot;stage&quot; : &quot;IXSCAN&quot;,</span><br><span class="line">                                                &quot;nReturned&quot; : 794,</span><br><span class="line">                                                &quot;executionTimeMillisEstimate&quot; : 20,</span><br><span class="line">                                                &quot;works&quot; : 795,</span><br><span class="line">                                                &quot;advanced&quot; : 794,</span><br><span class="line">                                                &quot;needTime&quot; : 0,</span><br><span class="line">                                                &quot;needYield&quot; : 0,</span><br><span class="line">                                                &quot;saveState&quot; : 13,</span><br><span class="line">                                                &quot;restoreState&quot; : 13,</span><br><span class="line">                                                &quot;isEOF&quot; : 1,</span><br><span class="line">                                                &quot;invalidates&quot; : 0,</span><br><span class="line">                                                &quot;keyPattern&quot; : &#123;</span><br><span class="line">                                                        &quot;last_name&quot; : 1</span><br><span class="line">                                                &#125;,</span><br><span class="line">                                                &quot;indexName&quot; : &quot;last_name_1&quot;,</span><br><span class="line">                                                &quot;isMultiKey&quot; : false,</span><br><span class="line">                                                &quot;multiKeyPaths&quot; : &#123;</span><br><span class="line">                                                        &quot;last_name&quot; : [ ]</span><br><span class="line">                                                &#125;,</span><br><span class="line">                                                &quot;isUnique&quot; : false,</span><br><span class="line">                                                &quot;isSparse&quot; : false,</span><br><span class="line">                                                &quot;isPartial&quot; : false,</span><br><span class="line">                                                &quot;indexVersion&quot; : 2,</span><br><span class="line">                                                &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                                                &quot;indexBounds&quot; : &#123;</span><br><span class="line">                                                        &quot;last_name&quot; : [</span><br><span class="line">                                                                &quot;[\&quot;Johnson\&quot;, \&quot;Johnson\&quot;]&quot;</span><br><span class="line">                                                        ]</span><br><span class="line">                                                &#125;,</span><br><span class="line">                                                &quot;keysExamined&quot; : 794,</span><br><span class="line">                                                &quot;seeks&quot; : 1,</span><br><span class="line">                                                &quot;dupsTested&quot; : 0,</span><br><span class="line">                                                &quot;dupsDropped&quot; : 0,</span><br><span class="line">                                                &quot;seenInvalidated&quot; : 0</span><br><span class="line">                                        &#125;</span><br><span class="line">                                &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;serverInfo&quot; : &#123;</span><br><span class="line">                &quot;host&quot; : &quot;YZJC1606-0218&quot;,</span><br><span class="line">                &quot;port&quot; : 27017,</span><br><span class="line">                &quot;version&quot; : &quot;3.4.7&quot;,</span><br><span class="line">                &quot;gitVersion&quot; : &quot;cf38c1b8a0a8dca4a11737581beafef4fe120bcd&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;ok&quot; : 1</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>查询共用时9ms， 查询了794个文档（全文检索），查询了794个索引，返回了794个结果。可以看到，查询使用到了索引 _last_name_1_（FETCH、IXSCAN），但是排序却是在内存中进行(SORT)，这也是十分消耗资源的操作。</li>
</ul>
<p>再新建first_name索引，看是否会对查询产生影响<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&gt; db.people.getIndexes()</span><br><span class="line">[</span><br><span class="line">        &#123;</span><br><span class="line">                &quot;v&quot; : 2,</span><br><span class="line">                &quot;key&quot; : &#123;</span><br><span class="line">                        &quot;_id&quot; : 1</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;name&quot; : &quot;_id_&quot;,</span><br><span class="line">                &quot;ns&quot; : &quot;m201.people&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">                &quot;v&quot; : 2,</span><br><span class="line">                &quot;key&quot; : &#123;</span><br><span class="line">                        &quot;last_name&quot; : 1</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;name&quot; : &quot;last_name_1&quot;,</span><br><span class="line">                &quot;ns&quot; : &quot;m201.people&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">                &quot;v&quot; : 2,</span><br><span class="line">                &quot;key&quot; : &#123;</span><br><span class="line">                        &quot;first_name&quot; : 1</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;name&quot; : &quot;first_name_1&quot;,</span><br><span class="line">                &quot;ns&quot; : &quot;m201.people&quot;</span><br><span class="line">        &#125;</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line">&gt; explain.find(&#123;last_name:&quot;Johnson&quot;&#125;).sort(&#123;first_name:1&#125;)</span><br><span class="line">&#123;</span><br><span class="line">        &quot;queryPlanner&quot; : &#123;</span><br><span class="line">                &quot;plannerVersion&quot; : 1,</span><br><span class="line">                &quot;namespace&quot; : &quot;m201.people&quot;,</span><br><span class="line">                &quot;indexFilterSet&quot; : false,</span><br><span class="line">                &quot;parsedQuery&quot; : &#123;</span><br><span class="line">                        &quot;last_name&quot; : &#123;</span><br><span class="line">                                &quot;$eq&quot; : &quot;Johnson&quot;</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;winningPlan&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;SORT&quot;,</span><br><span class="line">                        &quot;sortPattern&quot; : &#123;</span><br><span class="line">                                &quot;first_name&quot; : 1</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                &quot;stage&quot; : &quot;SORT_KEY_GENERATOR&quot;,</span><br><span class="line">                                &quot;inputStage&quot; : &#123;</span><br><span class="line">                                        &quot;stage&quot; : &quot;FETCH&quot;,</span><br><span class="line">                                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                                &quot;stage&quot; : &quot;IXSCAN&quot;,</span><br><span class="line">                                                &quot;keyPattern&quot; : &#123;</span><br><span class="line">                                                        &quot;last_name&quot; : 1</span><br><span class="line">                                                &#125;,</span><br><span class="line">                                                &quot;indexName&quot; : &quot;last_name_1&quot;,</span><br><span class="line">                                                &quot;isMultiKey&quot; : false,</span><br><span class="line">                                                &quot;multiKeyPaths&quot; : &#123;</span><br><span class="line">                                                        &quot;last_name&quot; : [ ]</span><br><span class="line">                                                &#125;,</span><br><span class="line">                                                &quot;isUnique&quot; : false,</span><br><span class="line">                                                &quot;isSparse&quot; : false,</span><br><span class="line">                                                &quot;isPartial&quot; : false,</span><br><span class="line">                                                &quot;indexVersion&quot; : 2,</span><br><span class="line">                                                &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                                                &quot;indexBounds&quot; : &#123;</span><br><span class="line">                                                        &quot;last_name&quot; : [</span><br><span class="line">                                                                &quot;[\&quot;Johnson\&quot;, \&quot;Johnson\&quot;]&quot;</span><br><span class="line">                                                        ]</span><br><span class="line">                                                &#125;</span><br><span class="line">                                        &#125;</span><br><span class="line">                                &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;rejectedPlans&quot; : [</span><br><span class="line">                        &#123;</span><br><span class="line">                                &quot;stage&quot; : &quot;FETCH&quot;,</span><br><span class="line">                                &quot;filter&quot; : &#123;</span><br><span class="line">                                        &quot;last_name&quot; : &#123;</span><br><span class="line">                                                &quot;$eq&quot; : &quot;Johnson&quot;</span><br><span class="line">                                        &#125;</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;inputStage&quot; : &#123;</span><br><span class="line">                                        &quot;stage&quot; : &quot;IXSCAN&quot;,</span><br><span class="line">                                        &quot;keyPattern&quot; : &#123;</span><br><span class="line">                                                &quot;first_name&quot; : 1</span><br><span class="line">                                        &#125;,</span><br><span class="line">                                        &quot;indexName&quot; : &quot;first_name_1&quot;,</span><br><span class="line">                                        &quot;isMultiKey&quot; : false,</span><br><span class="line">                                        &quot;multiKeyPaths&quot; : &#123;</span><br><span class="line">                                                &quot;first_name&quot; : [ ]</span><br><span class="line">                                        &#125;,</span><br><span class="line">                                        &quot;isUnique&quot; : false,</span><br><span class="line">                                        &quot;isSparse&quot; : false,</span><br><span class="line">                                        &quot;isPartial&quot; : false,</span><br><span class="line">                                        &quot;indexVersion&quot; : 2,</span><br><span class="line">                                        &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                                        &quot;indexBounds&quot; : &#123;</span><br><span class="line">                                                &quot;first_name&quot; : [</span><br><span class="line">                                                        &quot;[MinKey, MaxKey]&quot;</span><br><span class="line">                                                ]</span><br><span class="line">                                        &#125;</span><br><span class="line">                                &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                ]</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;executionStats&quot; : &#123;</span><br><span class="line">                &quot;executionSuccess&quot; : true,</span><br><span class="line">                &quot;nReturned&quot; : 794,</span><br><span class="line">                &quot;executionTimeMillis&quot; : 67,</span><br><span class="line">                &quot;totalKeysExamined&quot; : 794,</span><br><span class="line">                &quot;totalDocsExamined&quot; : 794,</span><br><span class="line">                &quot;executionStages&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;SORT&quot;,</span><br><span class="line">                        &quot;nReturned&quot; : 794,</span><br><span class="line">                        &quot;executionTimeMillisEstimate&quot; : 20,</span><br><span class="line">                        &quot;works&quot; : 1591,</span><br><span class="line">                        &quot;advanced&quot; : 794,</span><br><span class="line">                        &quot;needTime&quot; : 796,</span><br><span class="line">                        &quot;needYield&quot; : 0,</span><br><span class="line">                        &quot;saveState&quot; : 22,</span><br><span class="line">                        &quot;restoreState&quot; : 22,</span><br><span class="line">                        &quot;isEOF&quot; : 1,</span><br><span class="line">                        &quot;invalidates&quot; : 0,</span><br><span class="line">                        &quot;sortPattern&quot; : &#123;</span><br><span class="line">                                &quot;first_name&quot; : 1</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;memUsage&quot; : 314926,</span><br><span class="line">                        &quot;memLimit&quot; : 33554432,</span><br><span class="line">                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                &quot;stage&quot; : &quot;SORT_KEY_GENERATOR&quot;,</span><br><span class="line">                                &quot;nReturned&quot; : 794,</span><br><span class="line">                                &quot;executionTimeMillisEstimate&quot; : 10,</span><br><span class="line">                                &quot;works&quot; : 796,</span><br><span class="line">                                &quot;advanced&quot; : 794,</span><br><span class="line">                                &quot;needTime&quot; : 1,</span><br><span class="line">                                &quot;needYield&quot; : 0,</span><br><span class="line">                                &quot;saveState&quot; : 22,</span><br><span class="line">                                &quot;restoreState&quot; : 22,</span><br><span class="line">                                &quot;isEOF&quot; : 1,</span><br><span class="line">                                &quot;invalidates&quot; : 0,</span><br><span class="line">                                &quot;inputStage&quot; : &#123;</span><br><span class="line">                                        &quot;stage&quot; : &quot;FETCH&quot;,</span><br><span class="line">                                        &quot;nReturned&quot; : 794,</span><br><span class="line">                                        &quot;executionTimeMillisEstimate&quot; : 10,</span><br><span class="line">                                        &quot;works&quot; : 795,</span><br><span class="line">                                        &quot;advanced&quot; : 794,</span><br><span class="line">                                        &quot;needTime&quot; : 0,</span><br><span class="line">                                        &quot;needYield&quot; : 0,</span><br><span class="line">                                        &quot;saveState&quot; : 22,</span><br><span class="line">                                        &quot;restoreState&quot; : 22,</span><br><span class="line">                                        &quot;isEOF&quot; : 1,</span><br><span class="line">                                        &quot;invalidates&quot; : 0,</span><br><span class="line">                                        &quot;docsExamined&quot; : 794,</span><br><span class="line">                                        &quot;alreadyHasObj&quot; : 0,</span><br><span class="line">                                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                                &quot;stage&quot; : &quot;IXSCAN&quot;,</span><br><span class="line">                                                &quot;nReturned&quot; : 794,</span><br><span class="line">                                                &quot;executionTimeMillisEstimate&quot; : 10,</span><br><span class="line">                                                &quot;works&quot; : 795,</span><br><span class="line">                                                &quot;advanced&quot; : 794,</span><br><span class="line">                                                &quot;needTime&quot; : 0,</span><br><span class="line">                                                &quot;needYield&quot; : 0,</span><br><span class="line">                                                &quot;saveState&quot; : 22,</span><br><span class="line">                                                &quot;restoreState&quot; : 22,</span><br><span class="line">                                                &quot;isEOF&quot; : 1,</span><br><span class="line">                                                &quot;invalidates&quot; : 0,</span><br><span class="line">                                                &quot;keyPattern&quot; : &#123;</span><br><span class="line">                                                        &quot;last_name&quot; : 1</span><br><span class="line">                                                &#125;,</span><br><span class="line">                                                &quot;indexName&quot; : &quot;last_name_1&quot;,</span><br><span class="line">                                                &quot;isMultiKey&quot; : false,</span><br><span class="line">                                                &quot;multiKeyPaths&quot; : &#123;</span><br><span class="line">                                                        &quot;last_name&quot; : [ ]</span><br><span class="line">                                                &#125;,</span><br><span class="line">                                                &quot;isUnique&quot; : false,</span><br><span class="line">                                                &quot;isSparse&quot; : false,</span><br><span class="line">                                                &quot;isPartial&quot; : false,</span><br><span class="line">                                                &quot;indexVersion&quot; : 2,</span><br><span class="line">                                                &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                                                &quot;indexBounds&quot; : &#123;</span><br><span class="line">                                                        &quot;last_name&quot; : [</span><br><span class="line">                                                                &quot;[\&quot;Johnson\&quot;, \&quot;Johnson\&quot;]&quot;</span><br><span class="line">                                                        ]</span><br><span class="line">                                                &#125;,</span><br><span class="line">                                                &quot;keysExamined&quot; : 794,</span><br><span class="line">                                                &quot;seeks&quot; : 1,</span><br><span class="line">                                                &quot;dupsTested&quot; : 0,</span><br><span class="line">                                                &quot;dupsDropped&quot; : 0,</span><br><span class="line">                                                &quot;seenInvalidated&quot; : 0</span><br><span class="line">                                        &#125;</span><br><span class="line">                                &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;serverInfo&quot; : &#123;</span><br><span class="line">                &quot;host&quot; : &quot;YZJC1606-0218&quot;,</span><br><span class="line">                &quot;port&quot; : 27017,</span><br><span class="line">                &quot;version&quot; : &quot;3.4.7&quot;,</span><br><span class="line">                &quot;gitVersion&quot; : &quot;cf38c1b8a0a8dca4a11737581beafef4fe120bcd&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;ok&quot; : 1</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>因为最多使用单索引中的一个进行查询，所以Mongo帮我们优化了，使用_last_name_1_这个索引，而放弃了_first_name_1_这个索引。索引执行计划和上面的查询一致。</p>
<h6 id="复合索引对排序影响"><a href="#复合索引对排序影响" class="headerlink" title="复合索引对排序影响"></a>复合索引对排序影响</h6><p>1.单个索引正序逆序无影响</p>
<ol>
<li>多个索引遵循前缀索引规则<br>3.多个索引正序逆序要一致</li>
</ol>
<h4 id="索引排序时，顺序，逆序问题"><a href="#索引排序时，顺序，逆序问题" class="headerlink" title="索引排序时，顺序，逆序问题"></a>索引排序时，顺序，逆序问题</h4><h2 id="数组元素索引——多键索引"><a href="#数组元素索引——多键索引" class="headerlink" title="数组元素索引——多键索引"></a>数组元素索引——多键索引</h2><ol>
<li>为一个存储数组的键创建的索引。无需显示的指定索引为多键索引，如果键存储的数据为数组，则在创建索引时，会默认该键上的索引为多键索引。</li>
<li>如果复合索引中包含多键索引的话，那么复合索引最多只能包含一个多键索引。</li>
</ol>
<h2 id="索引在排序中使用的问题"><a href="#索引在排序中使用的问题" class="headerlink" title="索引在排序中使用的问题"></a>索引在排序中使用的问题</h2><ol>
<li>sort如果没有用到索引会在内存中排序，这是我们不希望看到的</li>
<li>如果索引是复合索引，则sort字段与索引字段顺序一致，则会用到所有。如<br>索引：a:1,b:1,c:1<br>db.test.find().sort({a:1,b:1,c:1})<br>db.test.find().sort({a:1,b:1})<br>db.test.find().sort({a:1})<br>以上都会用到索引（前缀索引只能从左便开始，都能被使用）<br>然而：db.test.find().sort({a:1,c:1,b:1}) 则无法用到索引。因为sort参数的顺序与索引的顺序不一致。</li>
<li><p>排序的模式（升序，降序）与索引的模式不一致，也会用到索引，只是使用索引的策略不同，相同则是forward去查找索引，如果相反，则是backward（向后）查找索引。这个字段在explain中的direction字段中会标志<br>注意，如果索引的模式不一致，且为复合索引时，需要所有的索引模式都相反，不能个别字段模式相反，个别字段模式相同，这样也无法使用到索引。如 a:1,b:1,则sort({a:1,b:1})和sort({a:-1,b:-1})可以用到索引，而sort({a:1,b-1}),sort({a:-1,b1})无法使用到索引</p>
</li>
<li><p>如果排序的键符合索引的键或者索引的键的前缀，那么MOngo可以使用索引来排序查询的结果。</p>
</li>
<li><p>非前缀的索引键排序。这种情况下，索引中排序再排序键的前面的所有键，查询语句中必须包含针对他们的相等匹配的条件(排序不发生在内存中)<br>db.data.find({a:5}).sort({b:1,c:1})  {a:1,b:1,c:1}<br>db.data.find({b:3, a:4}).sort({c:1}) {a:1,b:1,c:1}<br>db.data.find({a:5,b:{$lt:3}}).sort({b:1}) {a:1,b:1}</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&gt; explain.find(&#123;first_name:&quot;Martin&quot;,last_name:&quot;Johnson&quot;&#125;).sort(&#123;email:1&#125;)</span><br><span class="line">&#123;</span><br><span class="line">        &quot;queryPlanner&quot; : &#123;</span><br><span class="line">                &quot;plannerVersion&quot; : 1,</span><br><span class="line">                &quot;namespace&quot; : &quot;m201.people&quot;,</span><br><span class="line">                &quot;indexFilterSet&quot; : false,</span><br><span class="line">                &quot;parsedQuery&quot; : &#123;</span><br><span class="line">                        &quot;$and&quot; : [</span><br><span class="line">                                &#123;</span><br><span class="line">                                        &quot;first_name&quot; : &#123;</span><br><span class="line">                                                &quot;$eq&quot; : &quot;Martin&quot;</span><br><span class="line">                                        &#125;</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &#123;</span><br><span class="line">                                        &quot;last_name&quot; : &#123;</span><br><span class="line">                                                &quot;$eq&quot; : &quot;Johnson&quot;</span><br><span class="line">                                        &#125;</span><br><span class="line">                                &#125;</span><br><span class="line">                        ]</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;winningPlan&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;FETCH&quot;,</span><br><span class="line">                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                &quot;stage&quot; : &quot;IXSCAN&quot;,</span><br><span class="line">                                &quot;keyPattern&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : 1,</span><br><span class="line">                                        &quot;last_name&quot; : 1,</span><br><span class="line">                                        &quot;email&quot; : 1</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;indexName&quot; : &quot;first_name_1_last_name_1_email_1&quot;,</span><br><span class="line">                                &quot;isMultiKey&quot; : false,</span><br><span class="line">                                &quot;multiKeyPaths&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : [ ],</span><br><span class="line">                                        &quot;last_name&quot; : [ ],</span><br><span class="line">                                        &quot;email&quot; : [ ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;isUnique&quot; : false,</span><br><span class="line">                                &quot;isSparse&quot; : false,</span><br><span class="line">                                &quot;isPartial&quot; : false,</span><br><span class="line">                                &quot;indexVersion&quot; : 2,</span><br><span class="line">                                &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                                &quot;indexBounds&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : [</span><br><span class="line">                                                &quot;[\&quot;Martin\&quot;, \&quot;Martin\&quot;]&quot;</span><br><span class="line">                                        ],</span><br><span class="line">                                        &quot;last_name&quot; : [</span><br><span class="line">                                                &quot;[\&quot;Johnson\&quot;, \&quot;Johnson\&quot;]&quot;</span><br><span class="line">                                        ],</span><br><span class="line">                                        &quot;email&quot; : [</span><br><span class="line">                                                &quot;[MinKey, MaxKey]&quot;</span><br><span class="line">                                        ]</span><br><span class="line">                                &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;rejectedPlans&quot; : [ ]</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;executionStats&quot; : &#123;</span><br><span class="line">                &quot;executionSuccess&quot; : true,</span><br><span class="line">                &quot;nReturned&quot; : 3,</span><br><span class="line">                &quot;executionTimeMillis&quot; : 0,</span><br><span class="line">                &quot;totalKeysExamined&quot; : 3,</span><br><span class="line">                &quot;totalDocsExamined&quot; : 3,</span><br><span class="line">                &quot;executionStages&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;FETCH&quot;,</span><br><span class="line">                        &quot;nReturned&quot; : 3,</span><br><span class="line">                        &quot;executionTimeMillisEstimate&quot; : 0,</span><br><span class="line">                        &quot;works&quot; : 4,</span><br><span class="line">                        &quot;advanced&quot; : 3,</span><br><span class="line">                        &quot;needTime&quot; : 0,</span><br><span class="line">                        &quot;needYield&quot; : 0,</span><br><span class="line">                        &quot;saveState&quot; : 0,</span><br><span class="line">                        &quot;restoreState&quot; : 0,</span><br><span class="line">                        &quot;isEOF&quot; : 1,</span><br><span class="line">                        &quot;invalidates&quot; : 0,</span><br><span class="line">                        &quot;docsExamined&quot; : 3,</span><br><span class="line">                        &quot;alreadyHasObj&quot; : 0,</span><br><span class="line">                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                &quot;stage&quot; : &quot;IXSCAN&quot;,</span><br><span class="line">                                &quot;nReturned&quot; : 3,</span><br><span class="line">                                &quot;executionTimeMillisEstimate&quot; : 0,</span><br><span class="line">                                &quot;works&quot; : 4,</span><br><span class="line">                                &quot;advanced&quot; : 3,</span><br><span class="line">                                &quot;needTime&quot; : 0,</span><br><span class="line">                                &quot;needYield&quot; : 0,</span><br><span class="line">                                &quot;saveState&quot; : 0,</span><br><span class="line">                                &quot;restoreState&quot; : 0,</span><br><span class="line">                                &quot;isEOF&quot; : 1,</span><br><span class="line">                                &quot;invalidates&quot; : 0,</span><br><span class="line">                                &quot;keyPattern&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : 1,</span><br><span class="line">                                        &quot;last_name&quot; : 1,</span><br><span class="line">                                        &quot;email&quot; : 1</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;indexName&quot; : &quot;first_name_1_last_name_1_email_1&quot;,</span><br><span class="line">                                &quot;isMultiKey&quot; : false,</span><br><span class="line">                                &quot;multiKeyPaths&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : [ ],</span><br><span class="line">                                        &quot;last_name&quot; : [ ],</span><br><span class="line">                                        &quot;email&quot; : [ ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;isUnique&quot; : false,</span><br><span class="line">                                &quot;isSparse&quot; : false,</span><br><span class="line">                                &quot;isPartial&quot; : false,</span><br><span class="line">                                &quot;indexVersion&quot; : 2,</span><br><span class="line">                                &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                                &quot;indexBounds&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : [</span><br><span class="line">                                                &quot;[\&quot;Martin\&quot;, \&quot;Martin\&quot;]&quot;</span><br><span class="line">                                        ],</span><br><span class="line">                                        &quot;last_name&quot; : [</span><br><span class="line">                                                &quot;[\&quot;Johnson\&quot;, \&quot;Johnson\&quot;]&quot;</span><br><span class="line">                                        ],</span><br><span class="line">                                        &quot;email&quot; : [</span><br><span class="line">                                                &quot;[MinKey, MaxKey]&quot;</span><br><span class="line">                                        ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;keysExamined&quot; : 3,</span><br><span class="line">                                &quot;seeks&quot; : 1,</span><br><span class="line">                                &quot;dupsTested&quot; : 0,</span><br><span class="line">                                &quot;dupsDropped&quot; : 0,</span><br><span class="line">                                &quot;seenInvalidated&quot; : 0</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;serverInfo&quot; : &#123;</span><br><span class="line">                &quot;host&quot; : &quot;YZJC1606-0218&quot;,</span><br><span class="line">                &quot;port&quot; : 27017,</span><br><span class="line">                &quot;version&quot; : &quot;3.4.7&quot;,</span><br><span class="line">                &quot;gitVersion&quot; : &quot;cf38c1b8a0a8dca4a11737581beafef4fe120bcd&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;ok&quot; : 1</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&gt; explain.find().sort(&#123;first_name:1, last_name:1&#125;)</span><br><span class="line">&#123;</span><br><span class="line">        &quot;queryPlanner&quot; : &#123;</span><br><span class="line">                &quot;plannerVersion&quot; : 1,</span><br><span class="line">                &quot;namespace&quot; : &quot;m201.people&quot;,</span><br><span class="line">                &quot;indexFilterSet&quot; : false,</span><br><span class="line">                &quot;parsedQuery&quot; : &#123;</span><br><span class="line"></span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;winningPlan&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;FETCH&quot;,</span><br><span class="line">                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                &quot;stage&quot; : &quot;IXSCAN&quot;,</span><br><span class="line">                                &quot;keyPattern&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : 1,</span><br><span class="line">                                        &quot;last_name&quot; : 1,</span><br><span class="line">                                        &quot;email&quot; : 1</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;indexName&quot; : &quot;first_name_1_last_name_1_email_1&quot;,</span><br><span class="line">                                &quot;isMultiKey&quot; : false,</span><br><span class="line">                                &quot;multiKeyPaths&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : [ ],</span><br><span class="line">                                        &quot;last_name&quot; : [ ],</span><br><span class="line">                                        &quot;email&quot; : [ ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;isUnique&quot; : false,</span><br><span class="line">                                &quot;isSparse&quot; : false,</span><br><span class="line">                                &quot;isPartial&quot; : false,</span><br><span class="line">                                &quot;indexVersion&quot; : 2,</span><br><span class="line">                                &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                                &quot;indexBounds&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : [</span><br><span class="line">                                                &quot;[MinKey, MaxKey]&quot;</span><br><span class="line">                                        ],</span><br><span class="line">                                        &quot;last_name&quot; : [</span><br><span class="line">                                                &quot;[MinKey, MaxKey]&quot;</span><br><span class="line">                                        ],</span><br><span class="line">                                        &quot;email&quot; : [</span><br><span class="line">                                                &quot;[MinKey, MaxKey]&quot;</span><br><span class="line">                                        ]</span><br><span class="line">                                &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;rejectedPlans&quot; : [ ]</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;executionStats&quot; : &#123;</span><br><span class="line">                &quot;executionSuccess&quot; : true,</span><br><span class="line">                &quot;nReturned&quot; : 50475,</span><br><span class="line">                &quot;executionTimeMillis&quot; : 175,</span><br><span class="line">                &quot;totalKeysExamined&quot; : 50475,</span><br><span class="line">                &quot;totalDocsExamined&quot; : 50475,</span><br><span class="line">                &quot;executionStages&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;FETCH&quot;,</span><br><span class="line">                        &quot;nReturned&quot; : 50475,</span><br><span class="line">                        &quot;executionTimeMillisEstimate&quot; : 2312,</span><br><span class="line">                        &quot;works&quot; : 50476,</span><br><span class="line">                        &quot;advanced&quot; : 50475,</span><br><span class="line">                        &quot;needTime&quot; : 0,</span><br><span class="line">                        &quot;needYield&quot; : 0,</span><br><span class="line">                        &quot;saveState&quot; : 394,</span><br><span class="line">                        &quot;restoreState&quot; : 394,</span><br><span class="line">                        &quot;isEOF&quot; : 1,</span><br><span class="line">                        &quot;invalidates&quot; : 0,</span><br><span class="line">                        &quot;docsExamined&quot; : 50475,</span><br><span class="line">                        &quot;alreadyHasObj&quot; : 0,</span><br><span class="line">                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                &quot;stage&quot; : &quot;IXSCAN&quot;,</span><br><span class="line">                                &quot;nReturned&quot; : 50475,</span><br><span class="line">                                &quot;executionTimeMillisEstimate&quot; : 2312,</span><br><span class="line">                                &quot;works&quot; : 50476,</span><br><span class="line">                                &quot;advanced&quot; : 50475,</span><br><span class="line">                                &quot;needTime&quot; : 0,</span><br><span class="line">                                &quot;needYield&quot; : 0,</span><br><span class="line">                                &quot;saveState&quot; : 394,</span><br><span class="line">                                &quot;restoreState&quot; : 394,</span><br><span class="line">                                &quot;isEOF&quot; : 1,</span><br><span class="line">                                &quot;invalidates&quot; : 0,</span><br><span class="line">                                &quot;keyPattern&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : 1,</span><br><span class="line">                                        &quot;last_name&quot; : 1,</span><br><span class="line">                                        &quot;email&quot; : 1</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;indexName&quot; : &quot;first_name_1_last_name_1_email_1&quot;,</span><br><span class="line">                                &quot;isMultiKey&quot; : false,</span><br><span class="line">                                &quot;multiKeyPaths&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : [ ],</span><br><span class="line">                                        &quot;last_name&quot; : [ ],</span><br><span class="line">                                        &quot;email&quot; : [ ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;isUnique&quot; : false,</span><br><span class="line">                                &quot;isSparse&quot; : false,</span><br><span class="line">                                &quot;isPartial&quot; : false,</span><br><span class="line">                                &quot;indexVersion&quot; : 2,</span><br><span class="line">                                &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                                &quot;indexBounds&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : [</span><br><span class="line">                                                &quot;[MinKey, MaxKey]&quot;</span><br><span class="line">                                        ],</span><br><span class="line">                                        &quot;last_name&quot; : [</span><br><span class="line">                                                &quot;[MinKey, MaxKey]&quot;</span><br><span class="line">                                        ],</span><br><span class="line">                                        &quot;email&quot; : [</span><br><span class="line">                                                &quot;[MinKey, MaxKey]&quot;</span><br><span class="line">                                        ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;keysExamined&quot; : 50475,</span><br><span class="line">                                &quot;seeks&quot; : 1,</span><br><span class="line">                                &quot;dupsTested&quot; : 0,</span><br><span class="line">                                &quot;dupsDropped&quot; : 0,</span><br><span class="line">                                &quot;seenInvalidated&quot; : 0</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;serverInfo&quot; : &#123;</span><br><span class="line">                &quot;host&quot; : &quot;YZJC1606-0218&quot;,</span><br><span class="line">                &quot;port&quot; : 27017,</span><br><span class="line">                &quot;version&quot; : &quot;3.4.7&quot;,</span><br><span class="line">                &quot;gitVersion&quot; : &quot;cf38c1b8a0a8dca4a11737581beafef4fe120bcd&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;ok&quot; : 1</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&gt; explain.find().sort(&#123;first_name:1, last_name:-1&#125;)</span><br><span class="line">&#123;</span><br><span class="line">        &quot;queryPlanner&quot; : &#123;</span><br><span class="line">                &quot;plannerVersion&quot; : 1,</span><br><span class="line">                &quot;namespace&quot; : &quot;m201.people&quot;,</span><br><span class="line">                &quot;indexFilterSet&quot; : false,</span><br><span class="line">                &quot;parsedQuery&quot; : &#123;</span><br><span class="line"></span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;winningPlan&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;SORT&quot;,</span><br><span class="line">                        &quot;sortPattern&quot; : &#123;</span><br><span class="line">                                &quot;first_name&quot; : 1,</span><br><span class="line">                                &quot;last_name&quot; : -1</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                &quot;stage&quot; : &quot;SORT_KEY_GENERATOR&quot;,</span><br><span class="line">                                &quot;inputStage&quot; : &#123;</span><br><span class="line">                                        &quot;stage&quot; : &quot;COLLSCAN&quot;,</span><br><span class="line">                                        &quot;direction&quot; : &quot;forward&quot;</span><br><span class="line">                                &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;rejectedPlans&quot; : [ ]</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;executionStats&quot; : &#123;</span><br><span class="line">                &quot;executionSuccess&quot; : true,</span><br><span class="line">                &quot;nReturned&quot; : 50475,</span><br><span class="line">                &quot;executionTimeMillis&quot; : 294,</span><br><span class="line">                &quot;totalKeysExamined&quot; : 0,</span><br><span class="line">                &quot;totalDocsExamined&quot; : 50475,</span><br><span class="line">                &quot;executionStages&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;SORT&quot;,</span><br><span class="line">                        &quot;nReturned&quot; : 50475,</span><br><span class="line">                        &quot;executionTimeMillisEstimate&quot; : 2161,</span><br><span class="line">                        &quot;works&quot; : 100954,</span><br><span class="line">                        &quot;advanced&quot; : 50475,</span><br><span class="line">                        &quot;needTime&quot; : 50478,</span><br><span class="line">                        &quot;needYield&quot; : 0,</span><br><span class="line">                        &quot;saveState&quot; : 789,</span><br><span class="line">                        &quot;restoreState&quot; : 789,</span><br><span class="line">                        &quot;isEOF&quot; : 1,</span><br><span class="line">                        &quot;invalidates&quot; : 0,</span><br><span class="line">                        &quot;sortPattern&quot; : &#123;</span><br><span class="line">                                &quot;first_name&quot; : 1,</span><br><span class="line">                                &quot;last_name&quot; : -1</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;memUsage&quot; : 19977916,</span><br><span class="line">                        &quot;memLimit&quot; : 33554432,</span><br><span class="line">                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                &quot;stage&quot; : &quot;SORT_KEY_GENERATOR&quot;,</span><br><span class="line">                                &quot;nReturned&quot; : 50475,</span><br><span class="line">                                &quot;executionTimeMillisEstimate&quot; : 0,</span><br><span class="line">                                &quot;works&quot; : 50478,</span><br><span class="line">                                &quot;advanced&quot; : 50475,</span><br><span class="line">                                &quot;needTime&quot; : 2,</span><br><span class="line">                                &quot;needYield&quot; : 0,</span><br><span class="line">                                &quot;saveState&quot; : 789,</span><br><span class="line">                                &quot;restoreState&quot; : 789,</span><br><span class="line">                                &quot;isEOF&quot; : 1,</span><br><span class="line">                                &quot;invalidates&quot; : 0,</span><br><span class="line">                                &quot;inputStage&quot; : &#123;</span><br><span class="line">                                        &quot;stage&quot; : &quot;COLLSCAN&quot;,</span><br><span class="line">                                        &quot;nReturned&quot; : 50475,</span><br><span class="line">                                        &quot;executionTimeMillisEstimate&quot; : 0,</span><br><span class="line">                                        &quot;works&quot; : 50477,</span><br><span class="line">                                        &quot;advanced&quot; : 50475,</span><br><span class="line">                                        &quot;needTime&quot; : 1,</span><br><span class="line">                                        &quot;needYield&quot; : 0,</span><br><span class="line">                                        &quot;saveState&quot; : 789,</span><br><span class="line">                                        &quot;restoreState&quot; : 789,</span><br><span class="line">                                        &quot;isEOF&quot; : 1,</span><br><span class="line">                                        &quot;invalidates&quot; : 0,</span><br><span class="line">                                        &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                                        &quot;docsExamined&quot; : 50475</span><br><span class="line">                                &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;serverInfo&quot; : &#123;</span><br><span class="line">                &quot;host&quot; : &quot;YZJC1606-0218&quot;,</span><br><span class="line">                &quot;port&quot; : 27017,</span><br><span class="line">                &quot;version&quot; : &quot;3.4.7&quot;,</span><br><span class="line">                &quot;gitVersion&quot; : &quot;cf38c1b8a0a8dca4a11737581beafef4fe120bcd&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;ok&quot; : 1</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&gt; explain.find(&#123;last_name:&quot;Johnson&quot;,first_name:&quot;Martin&quot;&#125;).sort(&#123;email:1&#125;)</span><br><span class="line">&#123;</span><br><span class="line">        &quot;queryPlanner&quot; : &#123;</span><br><span class="line">                &quot;plannerVersion&quot; : 1,</span><br><span class="line">                &quot;namespace&quot; : &quot;m201.people&quot;,</span><br><span class="line">                &quot;indexFilterSet&quot; : false,</span><br><span class="line">                &quot;parsedQuery&quot; : &#123;</span><br><span class="line">                        &quot;$and&quot; : [</span><br><span class="line">                                &#123;</span><br><span class="line">                                        &quot;first_name&quot; : &#123;</span><br><span class="line">                                                &quot;$eq&quot; : &quot;Martin&quot;</span><br><span class="line">                                        &#125;</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &#123;</span><br><span class="line">                                        &quot;last_name&quot; : &#123;</span><br><span class="line">                                                &quot;$eq&quot; : &quot;Johnson&quot;</span><br><span class="line">                                        &#125;</span><br><span class="line">                                &#125;</span><br><span class="line">                        ]</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;winningPlan&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;FETCH&quot;,</span><br><span class="line">                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                &quot;stage&quot; : &quot;IXSCAN&quot;,</span><br><span class="line">                                &quot;keyPattern&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : 1,</span><br><span class="line">                                        &quot;last_name&quot; : 1,</span><br><span class="line">                                        &quot;email&quot; : 1</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;indexName&quot; : &quot;first_name_1_last_name_1_email_1&quot;,</span><br><span class="line">                                &quot;isMultiKey&quot; : false,</span><br><span class="line">                                &quot;multiKeyPaths&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : [ ],</span><br><span class="line">                                        &quot;last_name&quot; : [ ],</span><br><span class="line">                                        &quot;email&quot; : [ ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;isUnique&quot; : false,</span><br><span class="line">                                &quot;isSparse&quot; : false,</span><br><span class="line">                                &quot;isPartial&quot; : false,</span><br><span class="line">                                &quot;indexVersion&quot; : 2,</span><br><span class="line">                                &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                                &quot;indexBounds&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : [</span><br><span class="line">                                                &quot;[\&quot;Martin\&quot;, \&quot;Martin\&quot;]&quot;</span><br><span class="line">                                        ],</span><br><span class="line">                                        &quot;last_name&quot; : [</span><br><span class="line">                                                &quot;[\&quot;Johnson\&quot;, \&quot;Johnson\&quot;]&quot;</span><br><span class="line">                                        ],</span><br><span class="line">                                        &quot;email&quot; : [</span><br><span class="line">                                                &quot;[MinKey, MaxKey]&quot;</span><br><span class="line">                                        ]</span><br><span class="line">                                &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;rejectedPlans&quot; : [ ]</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;executionStats&quot; : &#123;</span><br><span class="line">                &quot;executionSuccess&quot; : true,</span><br><span class="line">                &quot;nReturned&quot; : 3,</span><br><span class="line">                &quot;executionTimeMillis&quot; : 0,</span><br><span class="line">                &quot;totalKeysExamined&quot; : 3,</span><br><span class="line">                &quot;totalDocsExamined&quot; : 3,</span><br><span class="line">                &quot;executionStages&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;FETCH&quot;,</span><br><span class="line">                        &quot;nReturned&quot; : 3,</span><br><span class="line">                        &quot;executionTimeMillisEstimate&quot; : 0,</span><br><span class="line">                        &quot;works&quot; : 4,</span><br><span class="line">                        &quot;advanced&quot; : 3,</span><br><span class="line">                        &quot;needTime&quot; : 0,</span><br><span class="line">                        &quot;needYield&quot; : 0,</span><br><span class="line">                        &quot;saveState&quot; : 0,</span><br><span class="line">                        &quot;restoreState&quot; : 0,</span><br><span class="line">                        &quot;isEOF&quot; : 1,</span><br><span class="line">                        &quot;invalidates&quot; : 0,</span><br><span class="line">                        &quot;docsExamined&quot; : 3,</span><br><span class="line">                        &quot;alreadyHasObj&quot; : 0,</span><br><span class="line">                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                &quot;stage&quot; : &quot;IXSCAN&quot;,</span><br><span class="line">                                &quot;nReturned&quot; : 3,</span><br><span class="line">                                &quot;executionTimeMillisEstimate&quot; : 0,</span><br><span class="line">                                &quot;works&quot; : 4,</span><br><span class="line">                                &quot;advanced&quot; : 3,</span><br><span class="line">                                &quot;needTime&quot; : 0,</span><br><span class="line">                                &quot;needYield&quot; : 0,</span><br><span class="line">                                &quot;saveState&quot; : 0,</span><br><span class="line">                                &quot;restoreState&quot; : 0,</span><br><span class="line">                                &quot;isEOF&quot; : 1,</span><br><span class="line">                                &quot;invalidates&quot; : 0,</span><br><span class="line">                                &quot;keyPattern&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : 1,</span><br><span class="line">                                        &quot;last_name&quot; : 1,</span><br><span class="line">                                        &quot;email&quot; : 1</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;indexName&quot; : &quot;first_name_1_last_name_1_email_1&quot;,</span><br><span class="line">                                &quot;isMultiKey&quot; : false,</span><br><span class="line">                                &quot;multiKeyPaths&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : [ ],</span><br><span class="line">                                        &quot;last_name&quot; : [ ],</span><br><span class="line">                                        &quot;email&quot; : [ ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;isUnique&quot; : false,</span><br><span class="line">                                &quot;isSparse&quot; : false,</span><br><span class="line">                                &quot;isPartial&quot; : false,</span><br><span class="line">                                &quot;indexVersion&quot; : 2,</span><br><span class="line">                                &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                                &quot;indexBounds&quot; : &#123;</span><br><span class="line">                                        &quot;first_name&quot; : [</span><br><span class="line">                                                &quot;[\&quot;Martin\&quot;, \&quot;Martin\&quot;]&quot;</span><br><span class="line">                                        ],</span><br><span class="line">                                        &quot;last_name&quot; : [</span><br><span class="line">                                                &quot;[\&quot;Johnson\&quot;, \&quot;Johnson\&quot;]&quot;</span><br><span class="line">                                        ],</span><br><span class="line">                                        &quot;email&quot; : [</span><br><span class="line">                                                &quot;[MinKey, MaxKey]&quot;</span><br><span class="line">                                        ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;keysExamined&quot; : 3,</span><br><span class="line">                                &quot;seeks&quot; : 1,</span><br><span class="line">                                &quot;dupsTested&quot; : 0,</span><br><span class="line">                                &quot;dupsDropped&quot; : 0,</span><br><span class="line">                                &quot;seenInvalidated&quot; : 0</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;serverInfo&quot; : &#123;</span><br><span class="line">                &quot;host&quot; : &quot;YZJC1606-0218&quot;,</span><br><span class="line">                &quot;port&quot; : 27017,</span><br><span class="line">                &quot;version&quot; : &quot;3.4.7&quot;,</span><br><span class="line">                &quot;gitVersion&quot; : &quot;cf38c1b8a0a8dca4a11737581beafef4fe120bcd&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;ok&quot; : 1</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="稀疏索引"><a href="#稀疏索引" class="headerlink" title="稀疏索引"></a>稀疏索引</h2><p> 因为Mongo使用灵活的数据模型，可能某些键在文档中并不一定存在，如果要为这些键建立索引时，则那些不存在键的文档就会造成资源的浪费。Mongo给我们提供了稀疏索引，该索引只会为有该键的文档建立索引。<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">db.collection.createIndex(&#123;key:1&#125;,&#123;sparse:true&#125;)</span><br></pre></td></tr></table></figure></p>
<h2 id="TTL索引"><a href="#TTL索引" class="headerlink" title="TTL索引"></a>TTL索引</h2><p>如果用Mongo代替redis做一些缓存数据的存储时，为了缓存数据查询的效率，会为这些数据建立索引，但是某些缓存数据都会设置过期时间，用来节约存储资源，或者安全性（某些不重要的日志，用户登录回话信息，实现锁的键等）。Mongo的TTL索引能很方便的解决我们这部分的需求。在建立索引的时候设置一个过期时间，该索引为TTL索引，扥到索引过期后，Mongo后自动移除集合中索引的文档。<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">db.collection.createIndex(&#123;key:1&#125;,&#123;expireAfterSeconds:3600&#125;)</span><br></pre></td></tr></table></figure></p>
<h2 id="查询时间的迷惑"><a href="#查询时间的迷惑" class="headerlink" title="查询时间的迷惑"></a>查询时间的迷惑</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&gt; explain.find(&#123;last_name:&quot;Johnson&quot;&#125;)</span><br><span class="line">&#123;</span><br><span class="line">        &quot;queryPlanner&quot; : &#123;</span><br><span class="line">                &quot;plannerVersion&quot; : 1,</span><br><span class="line">                &quot;namespace&quot; : &quot;m201.people&quot;,</span><br><span class="line">                &quot;indexFilterSet&quot; : false,</span><br><span class="line">                &quot;parsedQuery&quot; : &#123;</span><br><span class="line">                        &quot;last_name&quot; : &#123;</span><br><span class="line">                                &quot;$eq&quot; : &quot;Johnson&quot;</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;winningPlan&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;FETCH&quot;,</span><br><span class="line">                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                &quot;stage&quot; : &quot;IXSCAN&quot;,</span><br><span class="line">                                &quot;keyPattern&quot; : &#123;</span><br><span class="line">                                        &quot;last_name&quot; : 1</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;indexName&quot; : &quot;last_name_1&quot;,</span><br><span class="line">                                &quot;isMultiKey&quot; : false,</span><br><span class="line">                                &quot;multiKeyPaths&quot; : &#123;</span><br><span class="line">                                        &quot;last_name&quot; : [ ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;isUnique&quot; : false,</span><br><span class="line">                                &quot;isSparse&quot; : false,</span><br><span class="line">                                &quot;isPartial&quot; : false,</span><br><span class="line">                                &quot;indexVersion&quot; : 2,</span><br><span class="line">                                &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                                &quot;indexBounds&quot; : &#123;</span><br><span class="line">                                        &quot;last_name&quot; : [</span><br><span class="line">                                                &quot;[\&quot;Johnson\&quot;, \&quot;Johnson\&quot;]&quot;</span><br><span class="line">                                        ]</span><br><span class="line">                                &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;rejectedPlans&quot; : [ ]</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;executionStats&quot; : &#123;</span><br><span class="line">                &quot;executionSuccess&quot; : true,</span><br><span class="line">                &quot;nReturned&quot; : 794,</span><br><span class="line">                &quot;executionTimeMillis&quot; : 1406,</span><br><span class="line">                &quot;totalKeysExamined&quot; : 794,</span><br><span class="line">                &quot;totalDocsExamined&quot; : 794,</span><br><span class="line">                &quot;executionStages&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;FETCH&quot;,</span><br><span class="line">                        &quot;nReturned&quot; : 794,</span><br><span class="line">                        &quot;executionTimeMillisEstimate&quot; : 1342,</span><br><span class="line">                        &quot;works&quot; : 795,</span><br><span class="line">                        &quot;advanced&quot; : 794,</span><br><span class="line">                        &quot;needTime&quot; : 0,</span><br><span class="line">                        &quot;needYield&quot; : 0,</span><br><span class="line">                        &quot;saveState&quot; : 63,</span><br><span class="line">                        &quot;restoreState&quot; : 63,</span><br><span class="line">                        &quot;isEOF&quot; : 1,</span><br><span class="line">                        &quot;invalidates&quot; : 0,</span><br><span class="line">                        &quot;docsExamined&quot; : 794,</span><br><span class="line">                        &quot;alreadyHasObj&quot; : 0,</span><br><span class="line">                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                &quot;stage&quot; : &quot;IXSCAN&quot;,</span><br><span class="line">                                &quot;nReturned&quot; : 794,</span><br><span class="line">                                &quot;executionTimeMillisEstimate&quot; : 10,</span><br><span class="line">                                &quot;works&quot; : 795,</span><br><span class="line">                                &quot;advanced&quot; : 794,</span><br><span class="line">                                &quot;needTime&quot; : 0,</span><br><span class="line">                                &quot;needYield&quot; : 0,</span><br><span class="line">                                &quot;saveState&quot; : 63,</span><br><span class="line">                                &quot;restoreState&quot; : 63,</span><br><span class="line">                                &quot;isEOF&quot; : 1,</span><br><span class="line">                                &quot;invalidates&quot; : 0,</span><br><span class="line">                                &quot;keyPattern&quot; : &#123;</span><br><span class="line">                                        &quot;last_name&quot; : 1</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;indexName&quot; : &quot;last_name_1&quot;,</span><br><span class="line">                                &quot;isMultiKey&quot; : false,</span><br><span class="line">                                &quot;multiKeyPaths&quot; : &#123;</span><br><span class="line">                                        &quot;last_name&quot; : [ ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;isUnique&quot; : false,</span><br><span class="line">                                &quot;isSparse&quot; : false,</span><br><span class="line">                                &quot;isPartial&quot; : false,</span><br><span class="line">                                &quot;indexVersion&quot; : 2,</span><br><span class="line">                                &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                                &quot;indexBounds&quot; : &#123;</span><br><span class="line">                                        &quot;last_name&quot; : [</span><br><span class="line">                                                &quot;[\&quot;Johnson\&quot;, \&quot;Johnson\&quot;]&quot;</span><br><span class="line">                                        ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;keysExamined&quot; : 794,</span><br><span class="line">                                &quot;seeks&quot; : 1,</span><br><span class="line">                                &quot;dupsTested&quot; : 0,</span><br><span class="line">                                &quot;dupsDropped&quot; : 0,</span><br><span class="line">                                &quot;seenInvalidated&quot; : 0</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;serverInfo&quot; : &#123;</span><br><span class="line">                &quot;host&quot; : &quot;YZJC1606-0218&quot;,</span><br><span class="line">                &quot;port&quot; : 27017,</span><br><span class="line">                &quot;version&quot; : &quot;3.4.7&quot;,</span><br><span class="line">                &quot;gitVersion&quot; : &quot;cf38c1b8a0a8dca4a11737581beafef4fe120bcd&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;ok&quot; : 1</span><br><span class="line">&#125;</span><br><span class="line">&gt; explain.find(&#123;last_name:&quot;Johnson&quot;&#125;)</span><br><span class="line">&#123;</span><br><span class="line">        &quot;queryPlanner&quot; : &#123;</span><br><span class="line">                &quot;plannerVersion&quot; : 1,</span><br><span class="line">                &quot;namespace&quot; : &quot;m201.people&quot;,</span><br><span class="line">                &quot;indexFilterSet&quot; : false,</span><br><span class="line">                &quot;parsedQuery&quot; : &#123;</span><br><span class="line">                        &quot;last_name&quot; : &#123;</span><br><span class="line">                                &quot;$eq&quot; : &quot;Johnson&quot;</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;winningPlan&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;FETCH&quot;,</span><br><span class="line">                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                &quot;stage&quot; : &quot;IXSCAN&quot;,</span><br><span class="line">                                &quot;keyPattern&quot; : &#123;</span><br><span class="line">                                        &quot;last_name&quot; : 1</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;indexName&quot; : &quot;last_name_1&quot;,</span><br><span class="line">                                &quot;isMultiKey&quot; : false,</span><br><span class="line">                                &quot;multiKeyPaths&quot; : &#123;</span><br><span class="line">                                        &quot;last_name&quot; : [ ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;isUnique&quot; : false,</span><br><span class="line">                                &quot;isSparse&quot; : false,</span><br><span class="line">                                &quot;isPartial&quot; : false,</span><br><span class="line">                                &quot;indexVersion&quot; : 2,</span><br><span class="line">                                &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                                &quot;indexBounds&quot; : &#123;</span><br><span class="line">                                        &quot;last_name&quot; : [</span><br><span class="line">                                                &quot;[\&quot;Johnson\&quot;, \&quot;Johnson\&quot;]&quot;</span><br><span class="line">                                        ]</span><br><span class="line">                                &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;rejectedPlans&quot; : [ ]</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;executionStats&quot; : &#123;</span><br><span class="line">                &quot;executionSuccess&quot; : true,</span><br><span class="line">                &quot;nReturned&quot; : 794,</span><br><span class="line">                &quot;executionTimeMillis&quot; : 1,</span><br><span class="line">                &quot;totalKeysExamined&quot; : 794,</span><br><span class="line">                &quot;totalDocsExamined&quot; : 794,</span><br><span class="line">                &quot;executionStages&quot; : &#123;</span><br><span class="line">                        &quot;stage&quot; : &quot;FETCH&quot;,</span><br><span class="line">                        &quot;nReturned&quot; : 794,</span><br><span class="line">                        &quot;executionTimeMillisEstimate&quot; : 0,</span><br><span class="line">                        &quot;works&quot; : 795,</span><br><span class="line">                        &quot;advanced&quot; : 794,</span><br><span class="line">                        &quot;needTime&quot; : 0,</span><br><span class="line">                        &quot;needYield&quot; : 0,</span><br><span class="line">                        &quot;saveState&quot; : 6,</span><br><span class="line">                        &quot;restoreState&quot; : 6,</span><br><span class="line">                        &quot;isEOF&quot; : 1,</span><br><span class="line">                        &quot;invalidates&quot; : 0,</span><br><span class="line">                        &quot;docsExamined&quot; : 794,</span><br><span class="line">                        &quot;alreadyHasObj&quot; : 0,</span><br><span class="line">                        &quot;inputStage&quot; : &#123;</span><br><span class="line">                                &quot;stage&quot; : &quot;IXSCAN&quot;,</span><br><span class="line">                                &quot;nReturned&quot; : 794,</span><br><span class="line">                                &quot;executionTimeMillisEstimate&quot; : 0,</span><br><span class="line">                                &quot;works&quot; : 795,</span><br><span class="line">                                &quot;advanced&quot; : 794,</span><br><span class="line">                                &quot;needTime&quot; : 0,</span><br><span class="line">                                &quot;needYield&quot; : 0,</span><br><span class="line">                                &quot;saveState&quot; : 6,</span><br><span class="line">                                &quot;restoreState&quot; : 6,</span><br><span class="line">                                &quot;isEOF&quot; : 1,</span><br><span class="line">                                &quot;invalidates&quot; : 0,</span><br><span class="line">                                &quot;keyPattern&quot; : &#123;</span><br><span class="line">                                        &quot;last_name&quot; : 1</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;indexName&quot; : &quot;last_name_1&quot;,</span><br><span class="line">                                &quot;isMultiKey&quot; : false,</span><br><span class="line">                                &quot;multiKeyPaths&quot; : &#123;</span><br><span class="line">                                        &quot;last_name&quot; : [ ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;isUnique&quot; : false,</span><br><span class="line">                                &quot;isSparse&quot; : false,</span><br><span class="line">                                &quot;isPartial&quot; : false,</span><br><span class="line">                                &quot;indexVersion&quot; : 2,</span><br><span class="line">                                &quot;direction&quot; : &quot;forward&quot;,</span><br><span class="line">                                &quot;indexBounds&quot; : &#123;</span><br><span class="line">                                        &quot;last_name&quot; : [</span><br><span class="line">                                                &quot;[\&quot;Johnson\&quot;, \&quot;Johnson\&quot;]&quot;</span><br><span class="line">                                        ]</span><br><span class="line">                                &#125;,</span><br><span class="line">                                &quot;keysExamined&quot; : 794,</span><br><span class="line">                                &quot;seeks&quot; : 1,</span><br><span class="line">                                &quot;dupsTested&quot; : 0,</span><br><span class="line">                                &quot;dupsDropped&quot; : 0,</span><br><span class="line">                                &quot;seenInvalidated&quot; : 0</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;serverInfo&quot; : &#123;</span><br><span class="line">                &quot;host&quot; : &quot;YZJC1606-0218&quot;,</span><br><span class="line">                &quot;port&quot; : 27017,</span><br><span class="line">                &quot;version&quot; : &quot;3.4.7&quot;,</span><br><span class="line">                &quot;gitVersion&quot; : &quot;cf38c1b8a0a8dca4a11737581beafef4fe120bcd&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;ok&quot; : 1</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>问：同样使用last_name这个查询，为什么两次查询花费的时间差别这么大（1406:1）？</li>
<li>解释：我们知道Mongo是内存数据库，但又不同于redis那样的全是内存数据库，他会根据内存的使用情况，考虑是否将数据加载到内存（查询速度快的根本原因）。如果内存不够了，Mongo会考虑将一部分数据刷到磁盘，如果下次插叙内存中没有找到，则会到磁盘去查，然后将数据加载到内存，这也是为什么，第一次和第二次都有索引的情况下，查询效率差别如此之大的原因。</li>
</ul>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>在日后的工作中，对于查询的优化还是要多用explain来分析我们的查询计划，并且多查看Mongo文档对索引规则的解释。那么我们一定能用Mongo的索引给我们的工程带来更好的性能提升！</p>
<blockquote>
<p>参考<br><a href="https://segmentfault.com/a/1190000004263246" target="_blank" rel="external">https://segmentfault.com/a/1190000004263246</a><br><a href="https://university.mongodb.com/course/MongoDB/M201/2017_September/chapter/Chapter_1_Introduction/lesson/58ab5626d280e42669e6a604/tab/vertical_e8c258a86135" target="_blank" rel="external">https://university.mongodb.com/course/MongoDB/M201/2017_September/chapter/Chapter_1_Introduction/lesson/58ab5626d280e42669e6a604/tab/vertical_e8c258a86135</a><br><a href="https://docs.mongodb.com/manual/tutorial/sort-results-with-indexes/" target="_blank" rel="external">索引对排序的影响</a><br><a href="http://www.mongoing.com/docs/tutorial/sort-results-with-indexes.html" target="_blank" rel="external">索引对排序的影响·中文</a></p>
</blockquote>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/Mongo/" rel="tag"># Mongo</a>
          
            <a href="/blog/tags/Mongo索引/" rel="tag"># Mongo索引</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2017/12/15/Elasticsearch随记/" rel="next" title="Elasticsearch随记">
                <i class="fa fa-chevron-left"></i> Elasticsearch随记
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2017/12/18/redis哨兵模式搭建/" rel="prev" title="redis哨兵模式搭建">
                redis哨兵模式搭建 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zNjY5NS8xMzIzMA=="></div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/blog/images/head.jpg"
               alt="Fang ShuHao" />
          <p class="site-author-name" itemprop="name">Fang ShuHao</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
          
        </div>
        <nav class="site-state motion-element">
        
          
            <div class="site-state-item site-state-posts">
              <a href="/blog/archives">
                <span class="site-state-item-count">135</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-categories">
              <a href="/blog/categories">
                <span class="site-state-item-count">35</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/blog/tags">
                <span class="site-state-item-count">93</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#单索引"><span class="nav-number">1.</span> <span class="nav-text">单索引</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#无索引查询Johnson"><span class="nav-number">1.0.0.0.1.</span> <span class="nav-text">无索引查询Johnson</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#添加索引last-name-查询Johnson"><span class="nav-number">1.0.0.0.2.</span> <span class="nav-text">添加索引last_name,查询Johnson</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#只有一个单索引-last-name"><span class="nav-number">1.0.0.0.3.</span> <span class="nav-text">只有一个单索引 last_name</span></a></li></ol></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#复合索引"><span class="nav-number">2.</span> <span class="nav-text">复合索引</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#没有索引进行-复合查询"><span class="nav-number">2.0.0.0.1.</span> <span class="nav-text">没有索引进行 复合查询</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#添加复合索引-first-name-last-name-email进行查询"><span class="nav-number">2.0.0.0.2.</span> <span class="nav-text">添加复合索引 first_name_last_name_email进行查询</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#复合索引对单个条件查询的影响"><span class="nav-number">2.0.0.0.3.</span> <span class="nav-text">复合索引对单个条件查询的影响</span></a></li></ol></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#复合索引-前缀索引"><span class="nav-number">3.</span> <span class="nav-text">复合索引-前缀索引</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#加入排序后索引规则"><span class="nav-number">4.</span> <span class="nav-text">加入排序后索引规则</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#单索引对排序影响"><span class="nav-number">4.0.0.0.1.</span> <span class="nav-text">单索引对排序影响</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#复合索引对排序影响"><span class="nav-number">4.0.0.0.2.</span> <span class="nav-text">复合索引对排序影响</span></a></li></ol></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#索引排序时，顺序，逆序问题"><span class="nav-number">4.0.1.</span> <span class="nav-text">索引排序时，顺序，逆序问题</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数组元素索引——多键索引"><span class="nav-number">5.</span> <span class="nav-text">数组元素索引——多键索引</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#索引在排序中使用的问题"><span class="nav-number">6.</span> <span class="nav-text">索引在排序中使用的问题</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#稀疏索引"><span class="nav-number">7.</span> <span class="nav-text">稀疏索引</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#TTL索引"><span class="nav-number">8.</span> <span class="nav-text">TTL索引</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#查询时间的迷惑"><span class="nav-number">9.</span> <span class="nav-text">查询时间的迷惑</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#总结"><span class="nav-number"></span> <span class="nav-text">总结</span></a></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Fang ShuHao</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    
    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.0"></script>



  



  




	





  





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  



  
  

  

  

  

  


  

</body>
</html>
